Arrays?

Andrew Koenig ark at research.att.com
Wed Nov 13 16:08:54 EST 2002


Wojtek> Dnia Wed, 13 Nov 2002 19:15:28 -0000, e-tones.co.uk napisa³(a):
>> So im  looking to creat a list? If so, how do I make it 2 dimensional?

>>> a=[[]]*2
>>> a
>>> [[], []]
>>> 

Wojtek> Is that what you wanted?

I doubt it:

>>> a = [[]]*2
>>> a
[[], []]
>>> a[0].append(42)
>>> a
[[42], [42]]

And also:

>>> a = [[0, 0]]*2  
>>> a
[[0, 0], [0, 0]]
>>> a[0][0] = "Hello"
>>> a[1][1] = "world"
>>> a
[['Hello', 'world'], ['Hello', 'world']]
>>> a[0][1]
'world'
>>> a[1][0]
'Hello'

If you want something that acts like a two-dimensional array, you must
take pains to ensure that the higher-level elements refer to distinct
objects.  That is part of the reason I asked about the ultimate purpose
of the code.

-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark



More information about the Python-list mailing list