469,289 Members | 2,330 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,289 developers. It's quick & easy.

Need help initializing a list or tuple.

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[i][j] = i**2

Mar 6 '06 #1
14 1286
"KraftDiner" wrote:
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[i][j] = i**2


this might help:

http://www.python.org/doc/faq/progra...mensional-list

</F>

Mar 6 '06 #2
Thank you that worked great!

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

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

Mar 6 '06 #3
KraftDiner schrieb:
Thank you that worked great!

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

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


sum(a, [])

Diez
Mar 6 '06 #4
Strange behaviour...

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

Prints:

65025
65025

Mar 6 '06 #5
"KraftDiner" wrote:
Strange behaviour...

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[i][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>

Mar 6 '06 #6
Yes I missed the fine print that said that the code had 'side effects'
So:

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

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

Mar 6 '06 #7
KraftDiner wrote:
[[1][2][3], [4][5][6], [7][8][9]]


I'm confused. Is that a valid list?
Mar 6 '06 #8
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?

Mar 6 '06 #9
nope.. sorry its not...
I meant x = [[1,2,3],[3,4,5],[5,6,7]]

Mar 6 '06 #10
John Salerno wrote:
KraftDiner wrote:
[[1][2][3], [4][5][6], [7][8][9]]


I'm confused. Is that a valid list?


No. I'm assuming he meant [[1, 2, 3], [4, 5, 6], [7, 8, 9]].
Mar 6 '06 #11
KraftDiner wrote:
nope.. sorry its not...
I meant x = [[1,2,3],[3,4,5],[5,6,7]]


Whew, you were about to really throw me for a loop otherwise! :)
Mar 6 '06 #12
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) ]
a is c False a == c

True

BranoZ

Mar 6 '06 #13
# 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]]

Mar 6 '06 #14

"KraftDiner" <bo*******@yahoo.com> wrote in message
news:11**********************@j52g2000cwj.googlegr oups.com...
Thank you that worked great!

a = [[None] * 256] * 256
for i in range(0,256):
for j in range(0,256):
a[i][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

Mar 7 '06 #15

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Pierre Fortin | last post: by
3 posts views Thread by bruce | last post: by
reply views Thread by bruce | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.