PDA

View Full Version : how to declare a 2D array in python


krypto_wizard
February 10th, 2006, 06:44 PM
I am trying to execute the following code


a =[][]
for i in range(3):
for j in range(3):
a[i][j] = i+j

print a


I get an error "invalid syntax". How to fix this bug.

Thanks for the help

thumper
February 10th, 2006, 06:56 PM
You need to append values into the lists

>>> a = []
>>> for i in xrange(3):
... a.append([])
... for j in xrange(3):
... a[i].append(i+j)
...
>>> a
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
>>>

krypto_wizard
February 10th, 2006, 07:26 PM
Thanks, it worked like a charm.

You need to append values into the lists

>>> a = []
>>> for i in xrange(3):
... a.append([])
... for j in xrange(3):
... a[i].append(i+j)
...
>>> a
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
>>>

zobayer1
December 10th, 2010, 05:47 PM
:popcorn: Just to put, there is an easier way, have a look:

To create an n by n 2D array:
a = [[]*n for x in xrange(n)]
In case you want to initialize each element with some value v
a = [[v]*n for x in xrange(n)]
Test it

print a

Tony Flury
December 11th, 2010, 03:03 PM
if it is going to be a sparsley populated array, that you need to index with two integers, you might find that a dictionary with a tuple (x,y) as the key might work just as well.

ssam
December 11th, 2010, 03:12 PM
and if you are dealing with arrays of numbers then have a look at numpy.

Pipeliner
September 16th, 2011, 02:46 PM
You should also be able to type this in:
l_variable = [[[2]*3]*4]*5This will create a three dimensional array with the following properties


all values will be populated with '2'
the first dimension will be length 3
the second dimension will be length 4
the third dimension will be length 5

Because Python does not require type declaration when creating a new variable, I choose to put a prefix in front of my variables. In this instance I use the "l_" in front of my list variables.

This has been tested with Python 2.7.1

Senesence
September 16th, 2011, 03:19 PM
You should also be able to type this in:
l_variable = [[[2]*3]*4]*5This will create a three dimensional array

Yes, it will, but a three dimensional array of what?

Independent lists, or references to a single list?

This would be a good test to run:

l_variable[0][0][0] = 5
print l_variable

Be careful.

Pipeliner
September 16th, 2011, 03:29 PM
This will be the result of:
l_variable = [[[2]*3]*4]*5[[[2,2,2], [2,2,2], [2,2,2], [2,2,2]], [[2,2,2], [2,2,2], [2,2,2], [2,2,2]],[[2,2,2], [2,2,2], [2,2,2], [2,2,2]],[[2,2,2], [2,2,2], [2,2,2], [2,2,2]],[[2,2,2], [2,2,2], [2,2,2], [2,2,2]]]

Senesence
September 16th, 2011, 03:54 PM
Run this:


l_variable = [[[2]*3]*4]*5
print l_variable

l_variable[0][0][0] = 5
print l_variable


See the problem now?

Pipeliner
September 16th, 2011, 04:57 PM
Good Point,

Next time, instead of the hints, just post the error in the thread so everyone can follow.

Senesence
September 16th, 2011, 05:25 PM
Good Point,

Next time, instead of the hints, just post the error in the thread so everyone can follow.

It's not an error. The syntax is correct, and results in correct behavior, but it might be behavior that you didn't really expect, because you misunderstood the nature of the statement.

Anyway, I think hints are actually preferable to just dumped answers, because they teach people how to run their own tests, and think critically.

Also, it's not like these were "cryptic" hints; I essentially gave you the test to run, which would (with basic analysis) answer the questions posed.