Newbie: How do I implement an 2 element integer array in Python..?

Ricardo Gibert gibert at home.com
Wed Oct 11 13:34:42 EDT 2000


I couldn't find anything in the online docs about multi-dimensional arrays.
Apparently, you can only create one dimensional arrays, but you can still
"simulate" a 2 dimensional array:

>>> import array

To create a 3 by 5 array with elements initialized to the 4 byte value 0:

>>> MyArray = array.array('l',[0]*3*5)
>>> MyArray
array('l', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

Now lets change the last element 2,4 to the value one.

>>> i,j = 2,4
>>> MyArray[i + 3*j] = 1
>>> MyArray
array('l', [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1])

Note that the 'l' is the lower case of the letter "L".

I'm a Python newbie too. I hope this helps.


<jbranthoover at my-deja.com> wrote in message
news:8s1s48$89a$1 at nnrp1.deja.com...
> Hello All,
> What is the syntax for creating a 2 element integer array?  I
> have looked at the Array  Module documentation,  but I am afraid that I
> don't quite understand it.  In Basic I would do something like:
>
> Dim MyArray(100,16)
>
>
> For i = 0 to 100
> For j = 0 to 16
>       MyArray( i,j ) = MyData
>         Next j
> Next i
>
>
> Can someone out there give me a quick example of how this
> function is done in Python?  Thank you for you time and have a nice day.
>
>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.





More information about the Python-list mailing list