Two-dimensional arrays

monsterkodi monsterkodi at gmx.net
Mon May 26 20:48:14 EDT 2003


> A = [None]*3
>  for i in range(3):
>        A[i] = [None] * 2 

> w, h = 2, 3
>  A = map(lambda i,w=w: [None] * w, range(h))

> w,h = 2,3
>  A = [ [None]*w for i in range(h) ] 

> dim1, dim2 = 3,5
> multi = [ [ 0 for col in range(dim2)] for row in range(dim1) ] 

> array = [[0 for column in range(dim)] for row in range(dim)] 

> array = [[0]*dim for row in range(dim)] 

> This is a known pitfall for Python beginners.  You are perhaps the
> 10th person in the last several years to do and ask the same ;-)

And I am sure: there are plenty more afraid to ask or trying to comprehend the FAQ.

Anybody else here who thinks that this is one of the very few embarrasing sides of 
python?

Should this issue be worth another PEP?

Am I missing some deeper knowledge which would enable me to see the problem
in providing 'low level' 'c-stylish' 'built-in' multidimensional list features?

Feel free to blame me for my (just curious ;) questions,

yours kodi



P.S.: nevertheless how inappropriate my posting, it's relaxing to discover that
ruby seems to suffer similar problems :)

> arr = Array.new(2)
> arr.each_index { |i|
>    r = Array.new(2)
>     r.each_index { |j|
>         r[j] = 0
>     }
>     arr[i] = r
> }




More information about the Python-list mailing list