### Thread: Bidimensional arrays in Python

1. #### Bidimensional arrays in Python

Hi, my problem is simple : I'm trying to make some 2x2 matrix operations, similar to the C code like
Code:
`int a[4][15]`
(not necessarily int). And honestly, I have no clue about how to do that in Python. So how can I do that ?

I COULD make a "flat", one-dimensional array, and take each x'th element, but I'd be surprised if nobody ever worked with Python and matrices.
2. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Posts
6
Rep Power
0
Hello,

I'm not trained in Python but isn't :

Code:
```>>> a = [[1,2,3],[4,5,6],[7,8,9]]
>>> a[0]
[1, 2, 3]
>>> a[0][0]
1
>>> a[2][2]
9```
what you're looking for ? Just an array of arrays

Julien.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
133
Rep Power
12
Also, note that what you will be creating is not an array, but a list. A mutable list. You can't directly create an empty array, but you can of course create an array filled with default values:
Code:
`[[0 for i in range(2)] for i in range(2)]`
will return:
Code:
`[[0, 0], [0, 0]]`
Last edited by percivall; August 16th, 2003 at 07:40 AM.
4. Ah, that'll teach me to RTFM. Thanks.
5. No Profile Picture
Junior Member
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2003
Location
Alexandria, VA
Posts
5
Rep Power
0
Another way of doing the same thing:

Code:
`[ [0] * 2 ] * 2`
This is essentially the same as the list comprehension in the earlier post, but a little less typing.
6. Both do the same thing but making a list of 0 is pretty pointless . The first list compression does allow you to do allot more than the recusion though..

Code:
`[[i for i in range(10)] for i in range(2)]`
This will at least produce two list of numbers 0-9, but then why this would be useful I just don't know, ah maybe one day I will find a use for it

Thanks for the info though,

Take care guys,
Mark.
7. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jul 2003
Posts
133
Rep Power
12
List comprehension is most powerful when combined with a function, such as:
Code:
`[(lambda i: [i, chr(i)])(i) for i in range(65, 123)]`
This is of course also possible with a regular list comprehension...
Code:
`[[i, chr(i)] for i in range(65, 123)]`
Okay, not very useful. Anyway, it lets you do stuff in a nice way.
Last edited by percivall; August 19th, 2003 at 09:29 AM.
8. I'd be enclined to use the second . Smaller and and nicer to look at, plus since it does less (doesnt create a lambda) it should be slightly more efficent (in theory). Does give some idea of whats possible. Good for setting up lists .

good to know thanks,
Mark.
9. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Dec 2001
Location
Houston, TX
Posts
383
Rep Power
13
Originally posted by FiveGrainJa
Another way of doing the same thing:

Code:
`[ [0] * 2 ] * 2`
This is essentially the same as the list comprehension in the earlier post, but a little less typing.
Actually, you don't want to do this, because of the following:

Code:
```>>> a = [[0]*2]*2
>>> a
[[0, 0], [0, 0]]
>>> a[0][0] = 1
>>> a
[[1, 0], [1, 0]]
>>>```