LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-06-2007, 09:30 AM   #1
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Rep: Reputation: 15
python; multidimensional "arrays"


in python, when I want to use arrays, I follow this way;

DATA = [0] * nint

and when I want to use I do;

....

DATA[i] = ....

do you know how to do similar but in two dimensions ?

thanks
 
Old 12-06-2007, 05:49 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
maybe this
Code:
>>> e = [[0] * 3]*3
>>> e
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
 
Old 12-07-2007, 03:03 PM   #3
paddy3118
LQ Newbie
 
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by ghostdog74 View Post
maybe this
Code:
>>> e = [[0] * 3]*3
>>> e
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
You would duplicate references to the same list if you did the above.

Here is an example:
Code:
>>> e = [[0] * 3]*3
>>> e
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> e[0][1]
0
>>> e[0][1] =3.141
>>> e
[[0, 3.141, 0], [0, 3.141, 0], [0, 3.141, 0]]

Instead create a list of lists without duplicate references. Here is one way:
Code:
>>> e = [[[] for inner in range(3)] for outer in range(4)]
>>> e
[[[], [], []], [[], [], []], [[], [], []], [[], [], []]]
>>> len(e)
4
>>> len(e[0])
3
>>> for outer in range(4):
... 	for inner in range(3):
... 		e[outer][inner] = (outer,inner)
... 
>>> e
[[(0, 0), (0, 1), (0, 2)], [(1, 0), (1, 1), (1, 2)], [(2, 0), (2, 1), (2, 2)], [(3, 0), (3, 1), (3, 2)]]
>>>
- Paddy.
 
  


Reply

Tags
array, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can "Arrays" be used in shell scripting som_kurian Programming 6 12-05-2007 11:43 PM
dual OS install windows XPx64 linux RH4 on two RAID 1 arrays - "Grub read error" sdlinwwin Linux - Software 1 03-27-2007 01:28 PM
[python] print "\033[5;",x,"H=>" datbenik Programming 1 01-05-2006 05:12 AM
User "list" running process "python" TroelsSmit Linux - Newbie 2 02-22-2005 04:55 AM
resizing multidimensional arrays in C++ banerji Programming 2 07-12-2004 07:27 PM


All times are GMT -5. The time now is 11:30 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration