Re: populating a doubly-subscripted array

2010-11-09 Thread Mark Wooding
Gregory Ewing writes: > A reasonably elegant way to fix this is to use list comprehensions > for all except the innermost list: > >ff = [[0.0]*5 for i in xrange(5)] Yes, this is a good approach. I should have suggested something like this as a solution myself, rather than merely explaining

Re: populating a doubly-subscripted array

2010-11-09 Thread Gregory Ewing
Mark Wooding wrote: What [x] * n does is make a list, whose length is n, and all of whose entries are precisely the value x. If x itself is a list, then you get an outer list all of whose entries are the /same/ inner list A reasonably elegant way to fix this is to use list comprehensions for

Re: populating a doubly-subscripted array

2010-11-08 Thread Mark Wooding
g...@accutrol.com writes: > What am I missing? I am using Python 3.1.2. > > ff = [[0.0]*5]*5 > ff#(lists 5x5 array of 0.0) > for i in range(5): > for j in range(3): > ff[i][j] = i*10+j > print (i,j,ff[i][j]) # correctly prints ff array values > > ff

Re: populating a doubly-subscripted array

2010-11-08 Thread Ben Finney
g...@accutrol.com writes: > What am I missing? You're missing the fact that Python doesn't have a built-in “array” type, nor really “subscripts” for them. > ff = [[0.0]*5]*5 This creates a float object, ‘0.0’. It then creates a list containing five references to that same object. It then create

Re: populating a doubly-subscripted array

2010-11-08 Thread Robert Kern
On 11/8/10 3:56 PM, g...@accutrol.com wrote: What am I missing? I am using Python 3.1.2. ff = [[0.0]*5]*5 http://docs.python.org/faq/programming.html#how-do-i-create-a-multidimensional-list -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is