Author 
Message 
jainweiw #1 / 13

listdisplay semantics?
Hi all: I tried the oneline command in a interaction mode: [x for x in [1, 2, 3], y for y in [4, 5, 6]] and the result surprised me, that is: [[1,2,3],[1,2,3],[1,2,3],9,9,9] Who can explain the behavior? Since I expected the result should be: [[1,4],[1,5],[1,6],[2,4],...]  Pary All Rough Yet.

Fri, 28 Nov 2003 02:29:47 GMT 


Uwe Hoffman #2 / 13

listdisplay semantics?
Quote:
> Hi all: > I tried the oneline command in a interaction mode: > [x for x in [1, 2, 3], y for y in [4, 5, 6]]
actually is: [ x for x in [[1, 2, 3], y] for y in [4, 5, 6] ] Quote: > and the result surprised me, that is: > [[1,2,3],[1,2,3],[1,2,3],9,9,9] > Who can explain the behavior? > Since I expected the result should be: > [[1,4],[1,5],[1,6],[2,4],...]
[ [x,y] for x in [1, 2, 3] for y in [4, 5, 6] ] Quote: >  > Pary All Rough Yet.
regards uwe

Fri, 28 Nov 2003 02:50:44 GMT 


Roman Suz #3 / 13

listdisplay semantics?
Quote:
>Hi all: >I tried the oneline command in a interaction mode:
Try: del y [x for x in [1, 2, 3], y for y in [4, 5, 6]] Quote: >and the result surprised me, that is: >[[1,2,3],[1,2,3],[1,2,3],9,9,9] >Who can explain the behavior? >Since I expected the result should be: >[[1,4],[1,5],[1,6],[2,4],...] > >Pary All Rough Yet.
Sincerely yours, Roman Suzi 
_/ Sunday, June 10, 2001 _/ Powered by Linux RedHat 6.2 _/ _/ "Computers make very fast, very accurate mistakes." _/

Fri, 28 Nov 2003 02:50:47 GMT 


Emile van Sebill #4 / 13

listdisplay semantics?
I don't quite get the results you get, but from: print [[x,y] for x in [1,2,3] for y in [4,5,6]] I get [[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]] Which looks like what you want. From: print [x for x in [1, 2, 3], y for y in [4, 5, 6]] I get: [[1, 2, 3], [1, 2, 3], [1, 2, 3], 6, 6, 6] The difference is that the comma following [1,2,3] causes [1,2,3] to be treated as the first element of a tuple, and the then current value of y as the second element (in my case 6, in your case 9). Now your for y in [4,5,6] might as well be for i in range(3) as the y value in the loop is not used in the result. Note the subtle namespace issue. Your line of code then reads: "Give me a list of x's for x iterating over ([1,2,3] , y) three times. HTH,  Emile van Sebille

Quote: > Hi all: > I tried the oneline command in a interaction mode: > [x for x in [1, 2, 3], y for y in [4, 5, 6]] > and the result surprised me, that is: > [[1,2,3],[1,2,3],[1,2,3],9,9,9] > Who can explain the behavior? > Since I expected the result should be: > [[1,4],[1,5],[1,6],[2,4],...] >  > Pary All Rough Yet.

Fri, 28 Nov 2003 03:21:46 GMT 


Roman Suz #5 / 13

listdisplay semantics?
BTW, I think it could be listcomprehension's inconsistency (if not a bug!), look: 1>>> [x for x in [1, 2, 3], y for y in [4, 5, 6]] # first try Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: name 'y' is not defined 2>>> [x for x in [1, 2, 3], [y for y in [4, 5, 6]]] [[1, 2, 3], [4, 5, 6]] 3>>> [[x for x in [1, 2, 3]], [y for y in [4, 5, 6]]] [[1, 2, 3], [4, 5, 6]] 4>>> [x for x in [1, 2, 3], y for y in [4, 5, 6]] # 2nd try [[1, 2, 3], [1, 2, 3], [1, 2, 3], 6, 6, 6]  and this lead to subtle errors. There are even two things which seem wrong here: A) 1 and 4s steps differ only because y is now defined. B) results of step 2 and 3 do not differ, while visually I've added [] around first item. (it was a discovery to me to know that I can delimit {*filter*} list comprehensions by "," inside list definitions!) Sincerely yours, Roman Suzi 
_/ Sunday, June 10, 2001 _/ Powered by Linux RedHat 6.2 _/ _/ "Computers make very fast, very accurate mistakes." _/

Fri, 28 Nov 2003 03:00:30 GMT 


Tim Peter #6 / 13

listdisplay semantics?
[jainweiwu] Quote: > I tried the oneline command in a interaction mode: > [x for x in [1, 2, 3], y for y in [4, 5, 6]] > and the result surprised me, that is: > [[1,2,3],[1,2,3],[1,2,3],9,9,9] > Who can explain the behavior? > Since I expected the result should be: > [[1,4],[1,5],[1,6],[2,4],...]
It's "a feature", but a bad one, and I opened a bug report on it: <http://sf.net/tracker/index.php?func=detail&aid=431886&group_id=5470& atid=105470> The problem is that your original got parsed as: [x for x in ([1, 2, 3], y) for y in [4, 5, 6]] ^ ^ which is roughly equivalent to: temp = [] for x in ([1, 2, 3], y): for y in [4, 5, 6]: temp.append(x) That interpretation of the comma is surprising in context, and I expect python will change to disallow it. To get the effect you want: Quote: >>> [[x, y] for x in [1, 2, 3] for y in [4, 5, 6]]
[[1, 4], [1, 5], [1, 6], [2, 4], [2, 5], [2, 6], [3, 4], [3, 5], [3, 6]]

Fri, 28 Nov 2003 03:45:24 GMT 


Remco Gerli #7 / 13

listdisplay semantics?
Quote: > Hi all: > I tried the oneline command in a interaction mode: > [x for x in [1, 2, 3], y for y in [4, 5, 6]]
^ This is not the way to do what you want anyway, but it's the comma that gives the strange result; "[1, 2, 3], y" is a tuple of two elements. Quote: > and the result surprised me, that is: > [[1,2,3],[1,2,3],[1,2,3],9,9,9] > Who can explain the behavior?
What you wrote there means: [x for x in ([1, 2, 3], y) for y in [4, 5, 6]] So first x is [1, 2, 3], and it prints that for y=4, 5, 6 (the value of y doesn't have any effect, so it just prints [1, 2, 3] three times). Then x becomes y; and I *think* y was 9 before you wrote the list comprehension, so it wrote that. That's the only explanation I can think of there. Quote: > Since I expected the result should be: > [[1,4],[1,5],[1,6],[2,4],...]
You write that like this: [[x, y] for x in [1, 2, 3] for y in [4, 5, 6]]  Remco Gerlich

Fri, 28 Nov 2003 05:24:57 GMT 


Bengt Richt #8 / 13

listdisplay semantics?
Quote: >Hi all: >I tried the oneline command in a interaction mode: >[x for x in [1, 2, 3], y for y in [4, 5, 6]] >and the result surprised me, that is: >[[1,2,3],[1,2,3],[1,2,3],9,9,9] >Who can explain the behavior? >Since I expected the result should be: >[[1,4],[1,5],[1,6],[2,4],...] > >Pary All Rough Yet.
I think the first comma made your expression legal and equivalent to [x for x in ([1, 2, 3], y) for y in [4, 5, 6]] so x took on two values: [1, 2, 3] and the incoming value of y, which happened to be bound to a leftover 9 from some previous activity, because the bindings for the elements of the sequence ([1, 2, 3], y) were set up before the y loop got started. So the loop y sequenced 456 for for three elements of [1, 2, 3] and then x took on the value of y from the tuple which hadn't changed it was created, hence the 9, for three more elements of the final result. The actual values for the y loop make no difference except that the last will be available next time. Do a del y and you will get a NameError exception if you try to rerun your statement. Since you used y for the for expression, if you ran the expression again, you'd get 6's in the place of the 9's. >>> y='y' >>> [x for x in [1, 2, 3], y for y in [4, 5, 6]] [[1, 2, 3], [1, 2, 3], [1, 2, 3], 'y', 'y', 'y'] >>> [x for x in [1, 2, 3], y for y in [4, 5, 6]] [[1, 2, 3], [1, 2, 3], [1, 2, 3], 6, 6, 6] Then note the 6 left over from the first go above. Try the following (with [4,5,6]) to get what you expected. (I left off the 6 to avoid line wrap here :) >>> [[x,y] for x in[1,2,3] for y in [4,5]] [[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]] (caveat lector: I am neither Latinist nor Python guru yet ;)

Fri, 28 Nov 2003 05:54:27 GMT 


Bengt Richt #9 / 13

listdisplay semantics?
Quote:
>BTW, I think it could be listcomprehension's >inconsistency (if not a bug!), look: >1>>> [x for x in [1, 2, 3], y for y in [4, 5, 6]] # first try >Traceback (most recent call last): > File "<stdin>", line 1, in ? >NameError: name 'y' is not defined >2>>> [x for x in [1, 2, 3], [y for y in [4, 5, 6]]] >[[1, 2, 3], [4, 5, 6]] >3>>> [[x for x in [1, 2, 3]], [y for y in [4, 5, 6]]] >[[1, 2, 3], [4, 5, 6]] >4>>> [x for x in [1, 2, 3], y for y in [4, 5, 6]] # 2nd try >[[1, 2, 3], [1, 2, 3], [1, 2, 3], 6, 6, 6] > and this lead to subtle errors.
It is subtle, but not a bug per se, if you ask me. But it is like a combination of operator precedence and name scope traps for the unwary, which I was ;) Quote: >There are even two things which seem wrong here: >A) 1 and 4s steps differ only because y is now defined. >B) results of step 2 and 3 do not differ, while visually >I've added [] around first item.
I think it's the implicit tuplification again, but so long as that's legal we can't complain that 2+3 and 4+1 have the same result, even though the expressions look different ;) Quote: >2>>> [x for x in [1, 2, 3], [y for y in [4, 5, 6]]]
Tuplifying: >>> [x for x in ( [1, 2, 3], [y for y in [4, 5, 6]] )] [[1, 2, 3], [4, 5, 6]] or: >>> xtuple = ( [1, 2, 3], [y for y in [4, 5, 6]] ) >>> xtuple ([1, 2, 3], [4, 5, 6]) >>> [x for x in xtuple ] [[1, 2, 3], [4, 5, 6]] Quote: >3>>> [[x for x in [1, 2, 3]], [y for y in [4, 5, 6]]]
ISTM this one has to be legal, since it's just a list of expressions which individually happen to be (unambiguous) list comprehensions. I.e., >>> xcomp = [x for x in [1, 2, 3]] >>> ycomp = [y for y in [4, 5, 6]] >>> [[x for x in [1, 2, 3]], [y for y in [4, 5, 6]]] [[1, 2, 3], [4, 5, 6]] >>> [ xcomp, ycomp ] [[1, 2, 3], [4, 5, 6]] I hope I am not misleading anyone. I am still new to Python.

Fri, 28 Nov 2003 07:19:27 GMT 


Remco Gerli #10 / 13

listdisplay semantics?
(snippage) Quote: > 3>>> [[x for x in [1, 2, 3]], [y for y in [4, 5, 6]]] > [[1, 2, 3], [4, 5, 6]] > (it was a discovery to me to know that I can delimit > {*filter*} list comprehensions by "," inside list definitions!) >>> [1, 2] [1, 2] >>> [[1,2,3], [4,5,6]] [[1,2,3], [4,5,6]] >>> [[x for x in in [1, 2, 3]], [y for y in [4, 5, 6]]]
[[1,2,3], [4,5,6]] That is, you just rediscovered something you already knew :)  Remco Gerlich

Fri, 28 Nov 2003 15:28:43 GMT 


