Python - 2d Array's don't work.

22. February 2012 20:25

 

If you have been working with python you will notice that 2d array's just don't work. This might come as a surprise since almost every other modern programming language supports 2d array's. What might even come as a bigger surprise is that pythin doesn't support array's at all in its language. It uses lists to support this functionality. It might look like an array but it is actually a list.

 

This can be showen with the following. Note the error about the list.

 

 

arr = []
arr[0] = 1


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

 

 

So once you stop thinking about trying to create and array in pythin (which is easy since they don't exist) and start thinking about creating a list of items things become much easyier to understand why your 2d arrays of arr = [][] just doesnt work. As you have just attempts to create 2 lists and set a variable with them.

 

So to create a single array list is easy and we can do so using the following code.

 

>>> arr = range(0,5)
>>> print arr
[0, 1, 2, 3, 4]

>>> arr[0] = 1
>>> print arr
[1, 1, 2, 3, 4]

 

 

I actually find this really ugly. Since we just created a list of incrementing number and the array still needs to be set to zero. So to create a real array preset to zero you need to use the following.

 

 

>>> arr = []
>>> for i in range(0, 5):
...  arr.append(0)
...
>>> print arr
[0, 0, 0, 0, 0]

 

 

So moving onto the 2d array and you do exactly the same thing. Except you create a list and put it in the list item. Like this

 

 

>>> arr = []
>>> for i in range(0, 5):
...  x = []
...  for j in range(0, 5):
...   x.append(0)
...  arr.append(x)
...
>>> print arr
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]

 

Now you can read / write to the items by using an array like syntax or arr[0][0] .. arr[4][4].

 

There is another alternative support to using 2d arrays in python by using a single list and then calulating the offsets. However for the particular problem I was trying to solve it really didn't help since python doesn't appear to support proper for loops either!

 

I guess thats what you get with python!

E-mail Kick it! DZone it! del.icio.us Permalink


Comments (1) -

3/11/2012 2:42:09 AM #

Howdy, Python is no a C-like language, you have to think differently and learn a new way of thinking. A few tips:

Lists are not arrays
>>> l = ['name', 9, (True, False)]

>>> a = [0] * 5
>>> a
[0, 0, 0, 0, 0]

>>> a = [0 for i in range(0, 5)]
>>> a
[0, 0, 0, 0, 0]

>>> arr = []
>>> arr += [0]
>>> arr
[0]

Here, we create references of lists and not copies:
>>> m = [[0] * 5] * 5
>>> m
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

When you assign, it will follow those references:
>>> m[0][0] = 1
>>> m
[[1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0], [1, 0, 0, 0, 0]]

To avoid it, use a list comprehension:
>>> m = [[0] * 5 for i in range(5)]
>>> m
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>> m[0][0] = 1
>>> m
[[1, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

An alternative syntax for this:
>>> m = [0] * 5
>>> for i in range(5):
...   m[i] = [0] * 5
...
>>>

>>> for i in range(0,5):
...   for j in range(0,5):
...     print m[i][j],
...   print
...
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

Check out the "array" module in the std library if you need type consistency or NumPy if you need performance.

gv Italy |