By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,909 Members | 2,008 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,909 IT Pros & Developers. It's quick & easy.

2D array

P: n/a
I'm wanting to do something with a list that is basically a 2 dimensional
array. I'm not so good with lists so can someone give me an example of how I
might implement this in Python? thanks.
Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
LutherRevisited wrote:
I'm wanting to do something with a list that is basically a 2 dimensional
array. I'm not so good with lists so can someone give me an example of how I
might implement this in Python? thanks.


If you're planning to do anything serious with a 2D array, you should
probably look at numarray:
http://www.stsci.edu/resources/softw...dware/numarray
import numarray as na
arr = na.array(range(10), shape=(5, 2))
arr array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]]) arr[0,1] 1 arr[4,0] 8

If you're not doing any heavy computation, you can probably do this with
nested lists:
arr = [[0, 1], .... [2, 3],
.... [4, 5],
.... [6, 7],
.... [8, 9]] arr[0][1] 1 arr[4][0]

8

Steve
Jul 18 '05 #2

P: n/a
On Tue, 2004-12-07 at 23:02, Steven Bethard wrote:
LutherRevisited wrote:
I'm wanting to do something with a list that is basically a 2 dimensional
array. I'm not so good with lists so can someone give me an example of how I
might implement this in Python? thanks.


If you're planning to do anything serious with a 2D array, you should
probably look at numarray:
http://www.stsci.edu/resources/softw...dware/numarray
>>> import numarray as na
>>> arr = na.array(range(10), shape=(5, 2))
>>> arr array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]]) >>> arr[0,1] 1 >>> arr[4,0] 8

If you're not doing any heavy computation, you can probably do this with
nested lists:
>>> arr = [[0, 1], ... [2, 3],
... [4, 5],
... [6, 7],
... [8, 9]] >>> arr[0][1] 1 >>> arr[4][0] 8

Steve


If your data is sparse you might want to consider using a dictionary
where the key is a tuple representing the coordinates.

a = {}
a[(0,0)] = 0
a[(0,1)] = 1
a[(1,0)] = 2
a[(1,1)] = 3
a[(2,0)] = 4
a[(2,1)] = 5
a[(3,0)] = 6
a[(3,1)] = 7
a[(4,0)] = 8
a[(4,1)] = 9
a.get( (3,0), None ) 6 print a.get( (5,0), None )

None


Adam DePrince
Jul 18 '05 #3

P: n/a
Adam DePrince wrote:
If your data is sparse you might want to consider using a dictionary
where the key is a tuple representing the coordinates.

a = {}
a[(0,0)] = 0
a[(0,1)] = 1

[snip]
print a.get( (5,0), None )
Good point. Note that you don't need the parentheses in the assignments
or item accesses:
a = {}
a[0,0] = 10
a[0,0] 10

Also note that you don't need to specify None as the default value when
you call dict.get -- None is assumed if no default value is supplied:
print a.get((5, 2))

None

Steve
Jul 18 '05 #4

P: n/a
On Wed, 2004-12-08 at 15:06, Steven Bethard wrote:
Adam DePrince wrote:
If your data is sparse you might want to consider using a dictionary
where the key is a tuple representing the coordinates.

a = {}
a[(0,0)] = 0
a[(0,1)] = 1 [snip]
>print a.get( (5,0), None )
Good point. Note that you don't need the parentheses in the assignments
or item accesses:
>>> a = {}
>>> a[0,0] = 10
>>> a[0,0] 10

Also note that you don't need to specify None as the default value when
you call dict.get -- None is assumed if no default value is supplied:


The use of None as the default parameter was on purpose; the lack of
"magic" in python is often cited in religious wars between python and
perl aficionados. Use of get(something, None) was on purpose, the level
of familiarity with the language implied by the original question
suggested that the notion of optional parameters, and specifically those
of get, may not have been immediately obvious.

As for a[0,0] instead of a[(0,0)] ... the former just *looks* so
aesthetically wrong to me that I've never used it, and had forgotten
that it was even possible.
>>> print a.get((5, 2))

None

Steve

Adam DePrince
Jul 18 '05 #5

P: n/a
Adam DePrince wrote:
The use of None as the default parameter was on purpose; the lack of
"magic" in python is often cited in religious wars between python and
perl aficionados. Use of get(something, None) was on purpose, the level
of familiarity with the language implied by the original question
suggested that the notion of optional parameters, and specifically those
of get, may not have been immediately obvious.

As for a[0,0] instead of a[(0,0)] ... the former just *looks* so
aesthetically wrong to me that I've never used it, and had forgotten
that it was even possible.


Sorry, I hadn't meant any of my comments as criticisms -- just wanted to
make sure the OP knew about all the options open to them. I'm used to
a[0,0] because I've used numarray a bit, but to each his own, of course. =)

Steve
Jul 18 '05 #6

P: n/a
I am also not here to criticize style here, but I want to point
something out.

Something like a[1,2] might look wrong, but it's actually parsed
specially by Python to accommodate slicing of multidimensional arrays.
The difference is that, inside [], you can use slicing syntax, as in
a[1:2,3:4]. But using parentheses forces it to be parsed as an
ordinary tuple, where you can't use slicing syntax. Thus, a[(1:2,3:4)]
is a syntax error.

Obviously this is irrelevant for dicts. But if you're using some sort
of custom array object, that supports slicing in multiple dimensions,
you can't slice with the parentheses. Because of this, I don't use the
parentheses for things like multidimensional arrays.

I tend to use the parentheses whenever the index is some sort of atomic
value, however.

--
CARL BANKS

Jul 18 '05 #7

P: n/a
On Wed, 2004-12-08 at 16:22, Steven Bethard wrote:
Adam DePrince wrote:
The use of None as the default parameter was on purpose; the lack of
"magic" in python is often cited in religious wars between python and
perl aficionados. Use of get(something, None) was on purpose, the level
of familiarity with the language implied by the original question
suggested that the notion of optional parameters, and specifically those
of get, may not have been immediately obvious.

As for a[0,0] instead of a[(0,0)] ... the former just *looks* so
aesthetically wrong to me that I've never used it, and had forgotten
that it was even possible.


Sorry, I hadn't meant any of my comments as criticisms -- just wanted to
make sure the OP knew about all the options open to them. I'm used to
a[0,0] because I've used numarray a bit, but to each his own, of course. =)


Even if you were, there is certainly no need to apologize. In
hindsight, my response seems rather naive; as naive perhaps as the
students in my freshman year undergrad C class who having grown up on
Turbo pascal would add to their programs:

#define BEGIN {
#define END {

because it "looked right."
Adam DePrince
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.