473,320 Members | 2,003 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

2d lists

What is the best way to assign a 2d lists? Something like;

for i in range(10):
for j in range(10):
aList[i][j] = i*j

Thank you in advance.
Jul 18 '05 #1
4 2235
Depending on your purposes, you may be looking for numarray[1].
import numarray
anArray = numarray.fromfunction(lambda x, y: x*y, (10,10))
anArray

array([[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18],
[ 0, 3, 6, 9, 12, 15, 18, 21, 24, 27],
[ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36],
[ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45],
[ 0, 6, 12, 18, 24, 30, 36, 42, 48, 54],
[ 0, 7, 14, 21, 28, 35, 42, 49, 56, 63],
[ 0, 8, 16, 24, 32, 40, 48, 56, 64, 72],
[ 0, 9, 18, 27, 36, 45, 54, 63, 72, 81]])

A cheap trick is to use tuples as dict keys, which gets you a kind of
sparse N-dimensional array:
aDict = {}
for i in range(10):
for j in range(10):
aDict[i,j] = i*j

You can create your nested list with a nested list comprehension:
aList = [ [ i * j for i in range(10) ] for j in range(10) ]
if you really want lists.

Jeff
[1] http://www.google.com/search?q=numarray

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAuRScJd01MZaTXX0RAtbbAJ4vKiSbqueKMx5x+v/1LaIlt2MXQACglJru
kRqHjPrwKlDt3m1X/dzsPoE=
=4Q5T
-----END PGP SIGNATURE-----

Jul 18 '05 #2
SunX wrote:
What is the best way to assign a 2d lists? Something like;

for i in range(10):
for j in range(10):
aList[i][j] = i*j

Thank you in advance.


If you want a list of lists, you could do:

for i in range(10):
L = aList[i] = []
for j in range(10):
L[j] = i*j

A completely different option is to use a dict for 2d data:

d = {}

for i in range(10):
for j in range(10):
d[(i,j)] = i*j

Notice the index is a tuple.

--
Shalabh
Jul 18 '05 #3
Shalabh Chaturvedi wrote:
SunX wrote:
What is the best way to assign a 2d lists? Something like;

for i in range(10):
for j in range(10):
aList[i][j] = i*j

Thank you in advance.

If you want a list of lists, you could do:

for i in range(10):
L = aList[i] = []
for j in range(10):
L[j] = i*j


This is quite wrong. Here is a corrected version (this time tested :)

aList = []
for i in range(10):
L = []
aList.append(L)
for j in range(10):
L.append(i*j)

--
Shalabh
Jul 18 '05 #4
>>>>> xi*********@yahoo.com (SunX) (S) wrote:

S> What is the best way to assign a 2d lists? Something like;
S> for i in range(10):
S> for j in range(10):
S> aList[i][j] = i*j

S> Thank you in advance.

Use list comprehension:
aList = [[i*j for j in range(10)] for i in range(10)]
--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: P.***********@hccnet.nl
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
41
by: Odd-R. | last post by:
I have to lists, A and B, that may, or may not be equal. If they are not identical, I want the output to be three new lists, X,Y and Z where X has all the elements that are in A, but not in B, and...
3
by: s_subbarayan | last post by:
Dear all, 1)In one of our implementation for an application we are supposed to collate two linked lists.The actual problem is like this: There are two singularly linked lists, the final output...
1
by: Simon Forman | last post by:
I've got a function that I'd like to improve. It takes a list of lists and a "target" element, and it returns the set of the items in the lists that appear either before or after the target...
17
by: Gal Diskin | last post by:
Hi, I am writing a code that needs to iterate over 3 lists at the same time, i.e something like this: for x1 in l1: for x2 in l2: for x3 in l3: print "do something with", x1, x2, x3 What I...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
0
by: Joeyeti | last post by:
Hi fellow VB knowers (I am but a learner still). I have a question for you which I struggle with. I need to convert nested Lists in MS WORD (whether numbered or bulleted or mixed) from their...
3
by: =?Utf-8?B?QWRhbSBN?= | last post by:
Hi all, What is the syntax for a method that accepts a list of generic lists? For example, I have a List that contains the following lists: List<Car> List<Boat> List<Plane>
3
by: Qbert16 | last post by:
Hi, I'm quite new to python and am looking for help with lists inside lists This is an example I'm trying to do. I have the following thesaurus set... thesaurus = , ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.