[Tutor] Weird list indexing

Neil Schemenauer nas-pytut@python.ca
Wed Jul 9 17:26:09 2003


Zak Arntson wrote:
> My guess is that the three lists are all the same object, just pointed to
> three different times?

Right.

> So it seems my shortcut to create a two-dimensional array doesn't
> work. Is there better shortcut?

The best solution is probably to use the Numeric or Numarry extension.
Eg.

>>> import Numeric
>>> a = Numeric.zeros((5,5), 'O')
>>> a
array([[0 , 0 , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ]],'O')
>>> a[2][1] = 'foo'
>>> a
array([[0 , 0 , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ],
       [0 , foo , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ],
       [0 , 0 , 0 , 0 , 0 ]],'O')

The other solution is to use a 'for' loop (not really a short cut).

  Neil