How do you do arrays

Sean Blakey pythonista at gmail.com
Tue Feb 1 14:22:35 EST 2005


On Tue, 01 Feb 2005 10:52:45 -0800, Thomas Bunce <tbunce at 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])



More information about the Python-list mailing list