list-display semantics?
Emile van Sebille
emile at fenx.com
Sun Jun 10 15:21:46 EDT 2001
More information about the Python-list mailing list
Sun Jun 10 15:21:46 EDT 2001
- Previous message (by thread): BUG? list-comprehension's inconsistency? was: Re: list-display semantics?
- Next message (by thread): list-display semantics?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 emile at fenx.com --------- "jainweiwu" <parywu at seed.net.tw> wrote in message news:9g0dhl$if1$1 at news.nsysu.edu.tw... > Hi all: > I tried the one-line 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. > parywu at seed.net.tw > >
- Previous message (by thread): BUG? list-comprehension's inconsistency? was: Re: list-display semantics?
- Next message (by thread): list-display semantics?
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list