Thursday 15 September 2011

Python: Advanced Nested List Comprehension Syntax -



Python: Advanced Nested List Comprehension Syntax -

i playing around list comprehensions improve understanding of them , ran unexpected output not able explain. haven't found question asked before, if /is/ repeat question, apologize.

i trying write generator generated generators. simple generator uses list comprehension this:

(x x in range(10) if x%2==0) # generates integers in range(10)

what trying write generator generated 2 generators - first of generated numbers in range(10) , sec of generated odd numbers in range(10). this, did:

>>> (x x in range(10) if x%2==i in range(2)) <generator object <genexpr> @ 0x7f6b90948f00> >>> in g.next(): print ... traceback (most recent phone call last): file "<stdin>", line 1, in <module> file "<stdin>", line 1, in <genexpr> unboundlocalerror: local variable 'i' referenced before assignment >>> g.next() traceback (most recent phone call last): file "<stdin>", line 1, in <module> stopiteration >>> g = (x x in range(10) if x%2==i in range(2)) >>> g <generator object <genexpr> @ 0x7f6b90969730> >>> g.next() traceback (most recent phone call last): file "<stdin>", line 1, in <module> file "<stdin>", line 1, in <genexpr> unboundlocalerror: local variable 'i' referenced before assignment

i don't understand why 'i' beingness referenced before assignment

i thought might have had i in range(2), did:

>>> g = (x x in range(10) if x%2==i in [0.1]) >>> g <generator object <genexpr> @ 0x7f6b90948f00> >>> g.next() traceback (most recent phone call last): file "<stdin>", line 1, in <module> file "<stdin>", line 1, in <genexpr> unboundlocalerror: local variable 'i' referenced before assignment

this didn't create sense me, thought best seek simpler first. went lists , tried:

>>> [x x in range(10) if x%2==i in range(2)] [1, 1, 3, 3, 5, 5, 7, 7, 9, 9]

which expected same as:

>>> l = [] >>> in range(2): ... x in range(10): ... if x%2==i: ... l.append(x) ... >>> l [0, 2, 4, 6, 8, 1, 3, 5, 7, 9] # list comprehension malformed?

but when tried on hunch, worked:

>>> [[x x in range(10) if x%2==i] in range(2)] [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]] # nested lists in nested list comprehension somehow impact scope of if statements? :s

so thought might problem level of scope if statement operates in. tried this:

>>> [x x in range(10) in range(2) if x%2==i] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

and i'm thoroughly confused. can please explain behavior. don't understand why list comprehensions seem malformed, nor understand how scoping of if statements work.

any help greatly appreciated

thank you

ps: while proof-reading question, realized bit homework question - not.

you need utilize parentheses:

((x x in range(10) if x%2==i) in range(2))

this didn't create sense me, thought best seek simpler first. went lists , tried:

[>>> [x x in range(10) if x%2==i in range(2)] [1, 1, 3, 3, 5, 5, 7, 7, 9, 9]

that worked because previous list comprehension leaks variable enclosing scope, , become current one. seek starting fresh python interpreter, , fail due nameerror. counter's leaking behavior has been removed in python 3.

edit:

the equivalent loop for:

(x x in range(10) if x%2==i in range(2))

would be:

l = [] x in range(10): if x%2 == i: in range(2): l.append(x)

which gives name error.

edit2:

the parenthesed version:

((x x in range(10) if x%2==i) in range(2))

is equivalent to:

li = [] in range(2): lx = [] x in range(10): if x%2==i: lx.append(x) li.append(lx)

python syntax scope list-comprehension

No comments:

Post a Comment