455,537 Members | 1,440 Online Need help? Post your question and get tips & solutions from a community of 455,537 IT Pros & Developers. It's quick & easy.

# array of array of float

 P: n/a 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; now in Python, seems like we have to do something like a = [ [ ] ] * 200 and then just use a.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... Jul 9 '06 #1
6 Replies

 P: n/a Su************@gmail.com wrote: 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; now in Python, seems like we have to do something like a = [ [ ] ] * 200 and then just use a.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-c...mensional-list Jul 9 '06 #2

 P: n/a Su************@gmail.com wrote: 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; now in Python, seems like we have to do something like a = [ [ ] ] * 200 and then just use a.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/nu...ml#pgfId-60291 download: http://sourceforge.net/project/showf...kage_id=175103 main page: http://numeric.scipy.org/ Jul 9 '06 #3

 P: n/a Su************@gmail.com schrieb: 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; now in Python, seems like we have to do something like a = [ [ ] ] * 200 and then just use a.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)] Jul 9 '06 #4

 P: n/a Schüle Daniel schrieb: Su************@gmail.com schrieb: >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 usefloat a;now in Python, seems like we have to do something likea = [ [ ] ] * 200and then just usea.append(12.34) etcbut it turns out that all 200 elements points to the same list...and i have to usea = [ ]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)] Jul 9 '06 #5

 P: n/a 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. Jul 9 '06 #6

 P: n/a "Schüle Daniel" wrote: a = [[] for in range(200)] correction :) a = [[] for i in range(200)] the "*500" part still seems to be missing... Jul 12 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion. 