2D array

  • Thread starter LutherRevisited
  • Start date
L

LutherRevisited

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.
 
S

Steven Bethard

LutherRevisited said:
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/software_hardware/numarray
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
8

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

Steve
 
A

Adam DePrince

LutherRevisited said:
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/software_hardware/numarray
array([[0, 1],
[2, 3],
[4, 5],
[6, 7],
[8, 9]])
8

If you're not doing any heavy computation, you can probably do this with
nested lists:
... [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
None




Adam DePrince
 
S

Steven Bethard

Adam said:
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]

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:
None

Steve
 
A

Adam DePrince

Adam said:
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.
None

Steve
Adam DePrince
 
S

Steven Bethard

Adam said:
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
 
C

Carl Banks

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.
 
A

Adam DePrince

Adam said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top