array of array of float

S

Summercoolness

i used C too much and haven't used Python for a while...

like in C, if we want an array of array of float, we use

float a[200][500];

now in Python, seems like we have to do something like

a = [ [ ] ] * 200

and then just use

a[1].append(12.34) etc

but it turns out that all 200 elements points to the same list...
and i have to use

a = [ ]
for i in range (0, 200):
a.append([ ])

is there a simpler way... i wonder...
 
F

Fredrik Lundh

i used C too much and haven't used Python for a while...

like in C, if we want an array of array of float, we use

float a[200][500];

now in Python, seems like we have to do something like

a = [ [ ] ] * 200

and then just use

a[1].append(12.34) etc

but it turns out that all 200 elements points to the same list...
and i have to use

a = [ ]
for i in range (0, 200):
a.append([ ])

is there a simpler way... i wonder...

this is FAQ:

http://pyfaq.infogami.com/how-do-i-create-a-multidimensional-list

</F>
 
D

DH

i used C too much and haven't used Python for a while...

like in C, if we want an array of array of float, we use

float a[200][500];

now in Python, seems like we have to do something like

a = [ [ ] ] * 200

and then just use

a[1].append(12.34) etc

but it turns out that all 200 elements points to the same list...
and i have to use

a = [ ]
for i in range (0, 200):
a.append([ ])

is there a simpler way... i wonder...

Right, try the numpy module, and you can do:

from numpy import *
a = zeros((200,500), Float)

documentation:
http://numeric.scipy.org/numpydoc/numpy-6.html#pgfId-60291
download:
http://sourceforge.net/project/showfiles.php?group_id=1369&package_id=175103
main page:
http://numeric.scipy.org/
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

i used C too much and haven't used Python for a while...

like in C, if we want an array of array of float, we use

float a[200][500];

now in Python, seems like we have to do something like

a = [ [ ] ] * 200

and then just use

a[1].append(12.34) etc

but it turns out that all 200 elements points to the same list...
and i have to use

a = [ ]
for i in range (0, 200):
a.append([ ])

is there a simpler way... i wonder...

a = [[] for in range(200)]
 
?

=?ISO-8859-1?Q?Sch=FCle_Daniel?=

Schüle Daniel said:
i used C too much and haven't used Python for a while...

like in C, if we want an array of array of float, we use

float a[200][500];

now in Python, seems like we have to do something like

a = [ [ ] ] * 200

and then just use

a[1].append(12.34) etc

but it turns out that all 200 elements points to the same list...
and i have to use

a = [ ]
for i in range (0, 200):
a.append([ ])

is there a simpler way... i wonder...

a = [[] for in range(200)]

correction :)

a = [[] for i in range(200)]
 
T

tac-tics

Use nested list comprehensions:

matrix = [[0.0 for x in xrange(n)] for y in xrange(m)]

This is similar to "float matrix[m][n]" in C.

All cells are independent of each other in doing this.
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top