473,623 Members | 2,693 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 8314
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 multidimensiona l 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 multidimensiona l 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
2776
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 print_r cmd any suggestions would be great. Thanks much Todd //avShed array Array ( => Array ( => 1 => 08:00 ) => Array ( => 1 => 08:05 ) => Array ( => 1 => 08:10 ) => Array ( => 1 => 08:15 ) => Array ( => 1 => 08:20 ) => Array...
15
5172
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
3472
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( "name", "title", "reports to user id", "start date in the format: mm/dd/yyyy" ) ); How can I display this hierarchy in simple nested <li> tags in the most
12
55543
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 also removed) So far I have (val is value, ar is array, returns new array):
8
10212
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 it is assigned a text literal?? HOW can I change the array value to upper case then? What other method exists for arrays? Ex: var GridArrayName1 = new Array(); GridArrayName1 = new Array ('test-value'); GridArrayName1 = GridArrayName1...
58
10113
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 code... TCHAR myArray; DoStuff(myArray);
104
16921
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 that array Could you show me a little example how to do this? Thanks.
7
3189
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 up looking for characters that wrap around the stripped characters and it ends up as a recursive ordeal that fails to identify a poorly constructed $_GET variable (when someone hand-types the item into the line and makes a simple typing error).
17
7236
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 to show the array data to the end user. Can I do that? How?
0
8217
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8603
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8312
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7132
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6104
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4153
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2590
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1467
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.