Need help initializing a list or tuple.

K

KraftDiner

I need a matrix of 256 x 256 unsigned short values...
The matrix can be implemented as a list of lists or a tuple...
So for example:
[[1][2][3], [4][5][6], [7][8][9]]
or
((1,2,3),(4,5,6), (7,8,9))

This is what I have so far but its not working...

a = [][]
for i in range(0,256):
for j in range(0,256):
a[j] = i**2
 
K

KraftDiner

Thank you that worked great!

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[j] = i**2

Now.. Can I change this quickly into a 1 dimensional list of length
65536?
 
D

Diez B. Roggisch

KraftDiner said:
Thank you that worked great!

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[j] = i**2

Now.. Can I change this quickly into a 1 dimensional list of length
65536?


sum(a, [])

Diez
 
K

KraftDiner

Strange behaviour...

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[j] = i**2
a = sum(a, [])
print a[0]
print a[65535]

Prints:

65025
65025
 
F

Fredrik Lundh

KraftDiner said:
Strange behaviour...

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[j] = i**2
a = sum(a, [])
print a[0]
print a[65535]

Prints:

65025
65025


looks like you missed that the page I pointed you to explained *why*
the a = [[None] * 256] * 256 approach doesn't work...

</F>
 
K

KraftDiner

Yes I missed the fine print that said that the code had 'side effects'
So:

a = [None]*256
for i in range(256):
a = [None] * 256
for i in range(0,256):
for j in range(0,256):
a[j] = i**2

a = sum(a, [])
print a[0]
print a[65535]
 
K

KraftDiner

Oh by the way the b = sum(a, []) worked well to convert the two
dimensional array to a 1D array...
Is there a way to bring that 1D array back to a 2D array?
 
T

Terry Reedy

KraftDiner said:
Thank you that worked great!

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[j] = i**2

Now.. Can I change this quickly into a 1 dimensional list of length
65536?


If 'a' were a Numeric/Numarray/NumPy array, then I believe you could
re-dimensionalize it on the fly to be 1x65536 instead of 256x256. See
numeric.scipy.org for more.

tjr
 
B

BranoZ

How about:

a = [ [i**2 for j in range(256)] for i in range(256) ]
b = sum(a, [])
c = [ b[slice(i,i+256)] for i in range(0,256*256,256) ]True

BranoZ
 
C

cdunn2001

# Nested list comprehensions act like real for loops:
a = [[None for i in range(3)] for j in range(3)]
a [[None, None, None], [None, None, None], [None, None, None]]
a[0][0] = 5
a
[[5, None, None], [None, None, None], [None, None, None]]
# Side-effect: i and j will be changed.

# Another way (much less clear to my eyes):
a = map(lambda x: map(lambda x: None, range(3)), range(3))
a[0][0]=5
a
[[5, None, None], [None, None, None], [None, None, None]]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top