Two-dimensional arrays
François Pinard
pinard at iro.umontreal.ca
Mon May 26 13:04:19 EDT 2003
[Peter Slizik]
> I created the 2-dim array using
> >>> dim = 5
> >>> array = [[0]*dim]*dim
> [...] 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.
You might use:
array = [[0]*dim for row in range(dim)]
if you want all rows to be distinct. The general writing would be:
array = [[0 for column in range(dim)] for row in range(dim)]
in which you may replace `0' by a function of column and row.
--
François Pinard http://www.iro.umontreal.ca/~pinard
More information about the Python-list
mailing list