Two-dimensional arrays

David Eppstein eppstein at ics.uci.edu
Mon May 26 13:00:56 EDT 2003


In article <batgj9$3bfpi$1 at ID-196014.news.dfncis.de>,
 Peter Slizik <peter.slizik at pobox.sk> wrote:

> After a while of fooling around, I realised the problems is in 
> references.  The 2-dim array doesn't consist of five 1-dim arrays, but 
> of five references to the same array.
> 
> How can I overcome this problem? Or should I use some other approach to 
> solve this 2-dim array problem - so trivial in C, but 'unsolvable' in 
> Python?

The easiest way is to use a dict:

array = {}
array[1,2] = 1

But if you really want a list-of-lists without the duplication problem, 
one way to do it is:

array = [[0]*dim for i in range(dim)]
array[1][2] = 1

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science




More information about the Python-list mailing list