By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 1,251 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

How do you do arrays

P: n/a
I am new at Pyton and I am learning from book not classes
so please forgive my being slow

The below does not work I get an Error of File
"Matrix[index] = k
NameError: name 'iMatrix' is not defined"

while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

The book statement of
array(typecode, initializer) does not make sence
to me how it henerates ore relaes to the org name
for the array.

Thank You
Tom
Jul 18 '05 #1
Share this Question
Share on Google+
19 Replies


P: n/a
Tom,

Before you use iMatrix[index], you have to tell python to use iMatrix
as an array. You will do that using iMatrix = [] *outside* the loop.

iMatrix = []
while index < majorlop1: # rest of the loop statements

Since you are new, please take a look at the Python tutorial to get you
started.
http://docs.python.org/tut/tut.html

Thanks,
-Kartic

Jul 18 '05 #2

P: n/a
and it is called a "List" in Python parlance.

Jul 18 '05 #3

P: n/a
On Tue, 01 Feb 2005 10:52:45 -0800, Thomas Bunce <tb****@mac.com> wrote:
I am new at Pyton and I am learning from book not classes
so please forgive my being slow

The below does not work I get an Error of File
"Matrix[index] = k
NameError: name 'iMatrix' is not defined"

while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

The book statement of
array(typecode, initializer) does not make sence
to me how it henerates ore relaes to the org name
for the array.

Thank You
Tom
--
http://mail.python.org/mailman/listinfo/python-list

Like any other variable, you need to declare iMatrix before you use it:
$ python
Python 2.4 (#1, Dec 28 2004, 12:08:51)
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
import random
import array
index = 0
majorlop1 = 4
iMatrix = array.array('b')
listvalues = [1, 2, 3, 4]
while index < majorlop1: .... index = index + 1
.... k = random.choice(listvalues) + 1
.... iMatrix.append(k)
.... iMatrix array('b', [3, 5])


You should probably look at the wealth of information at
http://www.python.org/doc - the tutorial is a good start on how to use
the language, and the library reference has much more depth on the
array module.
http://docs.python.org/lib/module-array.html
--
Sean Blakey
Saint of Mild Amusement, Evil Genius, Big Geek
Python/Java/C++/C(Unix/Windows/Palm/Web) developer
quine = ['print "quine =",quine,"; exec(quine[0])"'] ; exec(quine[0])
Jul 18 '05 #4

P: n/a
Thomas Bunce wrote:
I am new at Pyton and I am learning from book not classes
so please forgive my being slow

The below does not work I get an Error of File
"Matrix[index] = k
NameError: name 'iMatrix' is not defined"

while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

The book statement of
array(typecode, initializer) does not make sence
to me how it henerates ore relaes to the org name
for the array.

Thank You
Tom


Thomas,
You can do
m = [4]
m

[4]

wes

Jul 18 '05 #5

P: n/a
Tom,

It has to be iMatrix.append(k), not iMatrix[index] = k. Python will
give an error - list assignment index out of range - for that.
Just curious - what book are you following?

-Kartic

Jul 18 '05 #6

P: n/a
Thanks all

Tom
Jul 18 '05 #7

P: n/a
If you want do numerical calculations with vectors and matrices, you
should probably use the Numarray module. Python's built-in lists are
not intended for heavy numerical computations. They are more flexible
than the arrays in languages like C and Fortran in that they can store
elements of different types. One can write, for example,
x = ["dog",1,2.3] .

Jul 18 '05 #8

P: n/a
Tryed it and this is what I got (I did go to the web sight)

tom(h=500)$ /tmp/501/Cleanup\ At\ Startup/ptesting-128981347.87.py.command; exit
Input the maximu number of tvalue: 114
Traceback (most recent call last):
File "/Users/tom/Desktop/ptesting.py", line 20, in ?
iMatrix[index] = k
IndexError: list assignment index out of range
logout
[Process completed]

The complete listing:

#!/usr/bin/python
import random
import sys
import array
#
### generates a list of numbers between 1 and target
### and uses 23 % of these values.
#

iMatrix = []

tvalue = input('Input the maximu number of tvalue: ')
majorlop1 = int tvalue * .23)
listvalues = range(tvalue)

sep = '- '
index = 0
while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

while index < majorlop1:
print '- %s %s' % (iMatrix[index], sep)
#
###
#
I would like to set the size of the List/array independent
of having to intialialize it prior to use.
If it help I will say the bad works I am using OSX

Thanks Tom
In article <11*********************@c13g2000cwb.googlegroups. com>,
"Kartic" <ka******************@gmail.com> wrote:
Tom,

Before you use iMatrix[index], you have to tell python to use iMatrix
as an array. You will do that using iMatrix = [] *outside* the loop.

iMatrix = []
while index < majorlop1: # rest of the loop statements

Since you are new, please take a look at the Python tutorial to get you
started.
http://docs.python.org/tut/tut.html

Thanks,
-Kartic

Jul 18 '05 #9

P: n/a
Thomas,
If you were allowed to do what you're doing, the
list first element would be getting skipped as "index"
is always > 0. The thing is, you don't want the "index"
var at all for adding to the list; just do jMatrix.append(k).
You can iterate over the list with
for x in jMatrix:
print x

Is it basic that indexes from 1 vs. 0? 'just about
completely forgotten basic.
wes

Thomas Bunce wrote:
Tryed it and this is what I got (I did go to the web sight)

tom(h=500)$ /tmp/501/Cleanup\ At\ Startup/ptesting-128981347.87.py.command; exit
Input the maximu number of tvalue: 114
Traceback (most recent call last):
File "/Users/tom/Desktop/ptesting.py", line 20, in ?
iMatrix[index] = k
IndexError: list assignment index out of range
logout
[Process completed]

The complete listing:

#!/usr/bin/python
import random
import sys
import array
#
### generates a list of numbers between 1 and target
### and uses 23 % of these values.
#

iMatrix = []

tvalue = input('Input the maximu number of tvalue: ')
majorlop1 = int tvalue * .23)
listvalues = range(tvalue)

sep = '- '
index = 0
while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

while index < majorlop1:
print '- %s %s' % (iMatrix[index], sep)
#
###
#
I would like to set the size of the List/array independent
of having to intialialize it prior to use.
If it help I will say the bad works I am using OSX

Thanks Tom
In article <11*********************@c13g2000cwb.googlegroups. com>,
"Kartic" <ka******************@gmail.com> wrote:

Tom,

Before you use iMatrix[index], you have to tell python to use iMatrix
as an array. You will do that using iMatrix = [] *outside* the loop.

iMatrix = []
while index < majorlop1: # rest of the loop statements

Since you are new, please take a look at the Python tutorial to get you
started.
http://docs.python.org/tut/tut.html

Thanks,
-Kartic


Jul 18 '05 #10

P: n/a
wes weston wrote:
Thomas,
If you were allowed to do what you're doing, the
list first element would be getting skipped as "index"
is always > 0. The thing is, you don't want the "index"
var at all for adding to the list; just do jMatrix.append(k).
You can iterate over the list with
for x in jMatrix:
print x

Is it basic that indexes from 1 vs. 0? 'just about
completely forgotten basic.
wes


I think most versions of Basic have arrays that by default start with
1, as did their ancestor, Fortran, although other lower bounds can be
specified. VB.NET, Microsoft's successor to Visual Basic, requires
arrays to start with zero.

Jul 18 '05 #11

P: n/a
Tom - I answered your question even before you posted it!

You have to use iMatrix.append(k) and NOT iMatrix[index] = k.

Also, what do you expect out of:
while index < majorlop1:
print '- %s %s' % ( iMatrix[index], sep)

This loop will never get executed because your previous loop finishes
due to the same condition index < majorlop1.

I am not sure what book you are using but I don't think it is a very
good one.
I would like to set the size of the List/array independent
of having to intialialize it prior to use.


You can do the following when you allocate your list.
iMatrix = [''] * size # where size is an integer
and this will give you a list of size empty elements. If you do this,
do not use iMatrix.append(). Use the array notation like you have in
you code currently.

Thanks
-Kartic

Jul 18 '05 #12

P: n/a
Learning Python O'Reilly book and Python In A Nut Shell and about 2 inchs
printed of Web information

Tom

In article <11**********************@f14g2000cwb.googlegroups .com>,
"Kartic" <ka******************@gmail.com> wrote:
Tom,

It has to be iMatrix.append(k), not iMatrix[index] = k. Python will
give an error - list assignment index out of range - for that.
Just curious - what book are you following?

-Kartic

Jul 18 '05 #13

P: n/a

It was when I saw a use of complex numbers as a usable statement I became
interested in Python

Tom
In article <11**********************@z14g2000cwz.googlegroups .com>,
be*******@aol.com wrote:
If you want do numerical calculations with vectors and matrices, you
should probably use the Numarray module. Python's built-in lists are
not intended for heavy numerical computations. They are more flexible
than the arrays in languages like C and Fortran in that they can store
elements of different types. One can write, for example,
x = ["dog",1,2.3] .

Jul 18 '05 #14

P: n/a
A solution that I haven't seen mentioned by other postings in the thread is
to implement the array as a dictionary:

iMatrix = {}
for index in range(majorlop1):
k = random.choice(listvalues) + 1
iMatrix[index] = k

Mind you, a dictionary does not behave *exactly* like an array. For
instance, in your example, you may later do a "del iMatrix[2]" and then you
wouldn't really be able to use iMatrix like an array anymore. But,
depending on your application, a dictionary may be perfectly suitable.

Hope this helps.

Dan

"Thomas Bunce" <tb****@mac.com> wrote in message
news:tb*********************@192.168.1.12...
I am new at Pyton and I am learning from book not classes
so please forgive my being slow

The below does not work I get an Error of File
"Matrix[index] = k
NameError: name 'iMatrix' is not defined"

while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

The book statement of
array(typecode, initializer) does not make sence
to me how it henerates ore relaes to the org name
for the array.

Thank You
Tom

Jul 18 '05 #15

P: n/a
Thanks
Tom
In article <-r********************@rogers.com>, "Dan Perl"
<da*****@mail-me.com> wrote:
A solution that I haven't seen mentioned by other postings in the thread is
to implement the array as a dictionary:

iMatrix = {}
for index in range(majorlop1):
k = random.choice(listvalues) + 1
iMatrix[index] = k

Mind you, a dictionary does not behave *exactly* like an array. For
instance, in your example, you may later do a "del iMatrix[2]" and then you
wouldn't really be able to use iMatrix like an array anymore. But,
depending on your application, a dictionary may be perfectly suitable.

Hope this helps.

Dan

"Thomas Bunce" <tb****@mac.com> wrote in message
news:tb*********************@192.168.1.12...
I am new at Pyton and I am learning from book not classes
so please forgive my being slow

The below does not work I get an Error of File
"Matrix[index] = k
NameError: name 'iMatrix' is not defined"

while index < majorlop1:
index = index + 1
k = random.choice(listvalues) + 1
iMatrix[index] = k

The book statement of
array(typecode, initializer) does not make sence
to me how it henerates ore relaes to the org name
for the array.

Thank You
Tom

Jul 18 '05 #16

P: n/a
On 1 Feb 2005 13:03:12 -0800, be*******@aol.com declaimed the following
in comp.lang.python:

I think most versions of Basic have arrays that by default start with
1, as did their ancestor, Fortran, although other lower bounds can be
Classic BASIC actually splits the difference.

dim a(10)

allocates 11 elements, indexed 0..10 -- but most classes tend to ignore
element 0, and algorithms are as if only 10 elements exist starting at
1.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #17

P: n/a
Dennis Lee Bieber wrote:
Classic BASIC actually splits the difference.

dim a(10)

allocates 11 elements, indexed 0..10 -- but most classes tend to ignore
element 0, and algorithms are as if only 10 elements exist starting at
1.


Basic also has the OPTION BASE instruction, which affects that.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
War is like love, it always finds a way.
-- Bertolt Brecht
Jul 18 '05 #18

P: n/a
Kartic <ka******************@gmail.com> wrote:
...
I am not sure what book you are using but I don't think it is a very
good one.


Hmmm, considering he said it's "Python in a Nutshell", I disagree with
you;-). If he had understood that he probably wanted to use lists, not
arrays, the top paragraph on p. 47 might have helped him -- "Assigning
to an item with an invalid index also raises an exception", and list
comprehensions are on p. 56.

But considering that in the first post he was just trying to assign to
an indexing on a name he had never bound at all, it seems to me that
starting with the Nutshell may simply have been a bit of "running before
you can walk". The Nutshell does mention that names (variables) spring
into existence when you bind them, which implies they don't exist
previously, and thus you can't perform any operation on them (such as
binding an indexing on them) before you're bound them; but the Nutshell
doesn't extensively belabor the point.

I tried to explain this issue to another poster who recently appeared to
want to turn the Nutshell into a lay version of the Heart Sutra
("Therefore, Shariputra, in Python there are no declarations. There is
no use strict, no option explicit, no implicit none; no type of a name
and no declaring of a name so that it can't be rebound, no defining of a
name's type and no omitting to define a name's type, ..."). In a quick
reference, there just isn't enough space to repeat every key point, much
less to belabor the implications of each; even if space were free,
having to skip through such repetitions would _waste_ time for the main
target audience -- people who know some Python and want to get reminded
of every detail about a certain subject, or just look up something
specific. Trying to make a book that's both a hand-holding tutorial
_and_ a quick reference would produce a book that is not very good at
either task, IMHO.
Alex
Jul 18 '05 #19

P: n/a
On Tue, 01 Feb 2005 23:12:57 -0800, Erik Max Francis <ma*@alcyone.com>
declaimed the following in comp.lang.python:

Basic also has the OPTION BASE instruction, which affects that.
I did say "classic"... Option Base came in with, what, GW-BASIC?

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #20

This discussion thread is closed

Replies have been disabled for this discussion.