Re: List of lists surprising behaviour

2010-06-18 Thread Stephen Hansen
On 6/18/10 8:40 AM, bart.c wrote: > I suppose there are pros and cons to both approaches; copying all the time > at least avoids some of the odd effects and inconsistencies you get using > Python: What inconsistencies? All your examples are perfectly consistent. Its just consistent to different id

Re: List of lists surprising behaviour

2010-06-18 Thread bart.c
"Steven D'Aprano" wrote in message news:4c1b8ac6$0$14148$c3e8...@news.astraweb.com... On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote: (Although I have an issue with the way that that append works. I tried it in another, simpler language (which always does deep copies): L:=(1,2,3) L append:

Re: List of lists surprising behaviour

2010-06-18 Thread Steven D'Aprano
On Fri, 18 Jun 2010 12:07:38 +0100, bart.c wrote: > (Although I have an issue with the way that that append works. I tried > it in another, simpler language (which always does deep copies): > > L:=(1,2,3) > L append:= L > print L > > output: (1,2,3,(1,2,3)) > > which is exactly what I'd expect

Re: List of lists surprising behaviour

2010-06-18 Thread bart.c
Lie Ryan wrote: On 06/18/10 20:00, bart.c wrote: (I don't know if Python allows circular references, but that would give problems anyway: how would you even print out such a list?) Python uses ellipsis to indicate recursive list: a = [1, 2, 3] a.append(a) a [1, 2, 3, [...]] Ok, perhaps w

Re: List of lists surprising behaviour

2010-06-18 Thread Lie Ryan
On 06/18/10 20:00, bart.c wrote: > (I > don't know if Python allows circular references, but that would give > problems anyway: how would you even print out such a list?) Python uses ellipsis to indicate recursive list: >>> a = [1, 2, 3] >>> a.append(a) >>> a [1, 2, 3, [...]] -- http://mail.pyt

Re: List of lists surprising behaviour

2010-06-18 Thread bart.c
Benjamin Kaplan wrote: On Thu, Jun 17, 2010 at 4:20 PM, bart.c wrote: I don't know how Python does things, but an object should either specify a special way of duplicating itself, or lend itself to some standard way of doing so. (So for a list, it's just a question of copying the data in the

Re: List of lists surprising behaviour

2010-06-17 Thread Steven D'Aprano
On Fri, 18 Jun 2010 00:20:30 +0100, bart.c wrote: > The code is clearly trying to set only t[0][0] to 1, not t[1][0] and > t[2][0] as well. Trying to guess the motivation of the person writing code is tricky, but in this case, that's a reasonable assumption. I can't think of any reason why some

Re: List of lists surprising behaviour

2010-06-17 Thread Lie Ryan
On 06/18/10 09:20, bart.c wrote: > > "J Kenneth King" wrote in message > news:87wrtxh0dq@agentultra.com... >> candide writes: >> >>> Let's the following code : >>> >> t=[[0]*2]*3 >> t >>> [[0, 0], [0, 0], [0, 0]] >> t[0][0]=1 >> t >>> [[1, 0], [1, 0], [1, 0]] >>> >>> Rather s

Re: List of lists surprising behaviour

2010-06-17 Thread rantingrick
On Jun 17, 6:44 pm, Benjamin Kaplan wrote: > It's the recursively duplicating each element that's the problem. How > do you know when to stop? Thats easy, stack overflow! ;-) -- http://mail.python.org/mailman/listinfo/python-list

Re: List of lists surprising behaviour

2010-06-17 Thread Benjamin Kaplan
On Thu, Jun 17, 2010 at 4:20 PM, bart.c wrote: > > "J Kenneth King" wrote in message > news:87wrtxh0dq@agentultra.com... >> >> candide writes: >> >>> Let's the following code : >>> >> t=[[0]*2]*3 >> t >>> >>> [[0, 0], [0, 0], [0, 0]] >> >> t[0][0]=1 >> t >>> >>> [[1, 0],

Re: List of lists surprising behaviour

2010-06-17 Thread bart.c
"J Kenneth King" wrote in message news:87wrtxh0dq@agentultra.com... candide writes: Let's the following code : t=[[0]*2]*3 t [[0, 0], [0, 0], [0, 0]] t[0][0]=1 t [[1, 0], [1, 0], [1, 0]] Rather surprising, isn't it ? Not at all, actually. The code is clearly trying to set only

Re: List of lists surprising behaviour

2010-06-17 Thread J Kenneth King
candide writes: > Let's the following code : > t=[[0]*2]*3 t > [[0, 0], [0, 0], [0, 0]] t[0][0]=1 t > [[1, 0], [1, 0], [1, 0]] > > Rather surprising, isn't it ? Not at all, actually. I'd be surprised if the multiplication operator was aware of object constructors. Even arr

Re: List of lists surprising behaviour

2010-06-17 Thread Boris Borcic
candide wrote: So what is the right way to initialize to 0 a 2D array ? Is that way correct : >>> t=[[0 for _ in range(2)] for _ in range(3)] That's overkill :) You can skip the inner loop by using a list display, eg t=[[0,0] for _ in range(3)] It seems there is no more trouble now :

Re: List of lists surprising behaviour

2010-06-17 Thread Matteo Landi
Yes you are. List comprehension makes you create list of lists without reference-sharing. You should also find a recipe about that on the python cookbook. On Thu, Jun 17, 2010 at 12:21 PM, candide wrote: > Let's the following code : > t=[[0]*2]*3 t > [[0, 0], [0, 0], [0, 0]] t[0][0

Re: List of lists surprising behaviour

2010-06-17 Thread Lie Ryan
On 06/17/10 20:21, candide wrote: > Let's the following code : > t=[[0]*2]*3 t > [[0, 0], [0, 0], [0, 0]] t[0][0]=1 t > [[1, 0], [1, 0], [1, 0]] > > Rather surprising, isn't it ? So I suppose all the subarrays reférence > the same array : > id(t[0]), id(t[1]), id(t[2]) >