arrays
Duncan Booth
duncan.booth at invalid.invalid
Sun Nov 21 07:00:04 EST 2004
Isaac To wrote:
>>>>>> "Rahul" == Rahul Garg <codedivine at gmail.com> writes:
>
> Rahul> Hi. Is there someway i can get something similar to
> Rahul> multi-dimensional arrays in python.I dont want to use
> Rahul> Numarray.
>
> Is an array of array sufficient? Thinks like [[0]*5]*3.
>
You were right to call that an array of array (singular): what you
suggested creates a list containing 3 references to the same list. I
suspect the OP is much more likely to want an array of arrays (plural).
The cleanest way these days is to use a list comprehension for all but the
innermost list (provided the initial value is immutable):
[[0]*5 for i in range(3)]
or, if you want something mutable or more complex for an initial value you
can just use list comprehensions throughout e.g.:
[ [(i*100+j) for i in range(5)] for j in range(3) ]
More information about the Python-list
mailing list