473,387 Members | 1,453 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,387 software developers and data experts.

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 1385
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Pierre Fortin | last post by:
Hi, Is the following a reasonable generic approach for converting python returned tuples/lists into dicts...? I'm not advocating library functions also return dicts (I'd probably spend more...
6
by: VonGuller | last post by:
Hello, I have a code like this(as an example) : public class class1:UserControl { private MyOwnCollectionBasedClass pList1; public MyOwnCollectionBasedClass List1 {
25
by: beginner | last post by:
Hi, I am wondering how do I 'flatten' a list or a tuple? For example, I'd like to transform or ] to . Another question is how do I pass a tuple or list of all the aurgements of a function to...
4
by: supermancc03 | last post by:
I have a List<Point> X(Point pos) which returns a list of coordinates of "neighboring cells" I also have a function getY(int x, int y) that takes in a set of points and returns me a double. I need...
3
by: bruce | last post by:
hi guys/gals... got a basic question that i can't get my hands around. i'm trying to programatically create/use a list/tuple (or whatever the right phrase in pyton is!!) basically,...
0
by: bruce | last post by:
Hi Fredrik... so, this still doesn't get me an array called 'cat', or 'dog' or do i somehow use stuff to add/extract the vals...??? thanks -----Original Message-----
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.