473,508 Members | 2,365 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

2D array

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
7 8304
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2757
by: Brian | last post by:
I'm diddlying with a script, and found some behavior I don't understand. Take this snippet: for ($i = 0; $i <= count($m); $i++) { array_shift($m); reset($m); }
2
575
by: Stormkid | last post by:
Hi Group I'm trying to figure out a way that I can take two (two dimensional) arrays and avShed and shed, and subtract the matching elements in shed from avShed I've pasted the arrays blow from a...
15
5156
by: lawrence | last post by:
I wanted to test xml_parse_into_struct() so I took the example off of www.php.net and put this code up on a site: <?php $simple = <<<END <item>
8
3465
by: vcardillo | last post by:
Hello all, Okay, I am having some troubles. What I am doing here is dealing with an employee hierarchy that is stored in an array. It looks like this: $employees = array( "user_id" => array(...
12
55527
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
8
10198
by: Mike S. Nowostawsky | last post by:
I tried using the "toUpperCase()" property to change the value of an array entity to uppercase BUT it tells me that the property is invalid. It seems that an array is not considered an object when...
58
10048
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
104
16852
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
7
3168
by: Jim Carlock | last post by:
Looking for suggestions on how to handle bad words that might get passed in through $_GET variables. My first thoughts included using str_replace() to strip out such content, but then one ends...
17
7211
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.