在第一个LC的末尾
i分配给
[8,9]:
>>> lis = [[1, 2, 3, 4, 5, 6], [4, 5, 6], [7], [8, 9]]>>> [j for i in lis for j in i][1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9]>>> i[8, 9]
现在,在第二个LC中,您需要对此进行迭代
i:
>>> [j for j in i for i in lis][8, 8, 8, 8, 9, 9, 9, 9]
这两个LC(大致)等效于:
>>> lis = [[1, 2, 3, 4, 5, 6], [4, 5, 6], [7], [8, 9]]>>> for i in lis:... for j in i:... print j,... 1 2 3 4 5 6 4 5 6 7 8 9>>> i[8, 9]>>> for j in i:... for i in lis:... print j,... 8 8 8 8 9 9 9 9
该问题已在py3.x中修复 :
特别是循环控制变量不再泄漏到周围的范围内。
演示(py 3.x):
>>> lis = [[1, 2, 3, 4, 5, 6], [4, 5, 6], [7], [8, 9]]>>> [j for i in lis for j in i][1, 2, 3, 4, 5, 6, 4, 5, 6, 7, 8, 9]>>> iTraceback (most recent call last):NameError: name 'i' is not defined>>> jTraceback (most recent call last):NameError: name 'j' is not defined
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)