How do you make long list comprehensions in python? -
for example:
>>> [x x in range(y) y in range(z) z in range(3)] traceback (most recent call last): file "<stdin>", line 1, in <module> nameerror: name 'y' not defined
i expect behave same as:
>>> a=[] >>> z in range(3): ... y in range(z): ... x in range(3): ... a.append(x) ... >>> [0, 1, 2, 0, 1, 2, 0, 1, 2]
but doesn't. why?
your current comprehension work if reverse order of loops:
[x z in range(3) y in range(z) x in range(3)]
Comments
Post a Comment