例如:
>>> [x for x in range(y) for y in range(z) for z in range(3)]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'y' is not defined
我希望这与以下行为相同:
>>> a=[]
>>> for z in range(3):
... for y in range(z):
... for x in range(3):
... a.append(x)
...
>>> a
[0, 1, 2, 0, 1, 2, 0, 1, 2]
但事实并非如此。为什么?