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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
12-06-2007, 09:30 AM
|
#1
|
|
Member
Registered: Dec 2007
Posts: 61
Rep:
|
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
|
|
|
|
12-06-2007, 05:49 PM
|
#2
|
|
Senior Member
Registered: Aug 2006
Posts: 2,695
|
maybe this
Code:
>>> e = [[0] * 3]*3
>>> e
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
|
|
|
|
12-07-2007, 03:03 PM
|
#3
|
|
LQ Newbie
Registered: Jul 2007
Location: Bristol UK
Distribution: RedHat, Cygwin
Posts: 14
Rep:
|
Quote:
Originally Posted by ghostdog74
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:25 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|