How do you do arrays

Dan Perl danperl at rogers.com
Tue Feb 1 17:29:18 EST 2005


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" <tbunce at mac.com> wrote in message 
news:tbunce-0102051052450001 at 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 





More information about the Python-list mailing list