[英]Understanding List Comprehensions in Python
在阅读官方教程时 ,我遇到了以下示例:
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
我听不懂,所以做了一些实验:
>>> [num for elem in vec]
[9, 9, 9]
>>> [num for elem in (vec for num in elem)]
[9, 9, 9]
而且我现在更加困惑!
因此,我的问题是:应该以什么顺序阅读列表理解?
编辑:我确定我没有在任何地方定义数值为9的num
变量。
sunqingyaos-MacBook-Air:Documents sunqingyao$ python
Python 2.7.10 (default, Oct 23 2015, 19:19:21)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.59.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> num
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [num for elem in vec]
[9, 9, 9]
>>> [num for elem in (vec for num in elem)]
[9, 9, 9]
>>>
列表理解中的循环从左到右读取。 如果您的列表理解将被写为普通循环,它将看起来像这样:
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> l = []
>>> for elem in vec:
... for num in elem:
... l.append(num)
...
>>> l
[1, 2, 3, 4, 5, 6, 7, 8, 9]
在Python 2中,列表推导中的变量共享外部范围,因此num
可在以后使用:
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> num
9
请注意,在Python 3上,行为是不同的:
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> num
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'num' is not defined
别担心 :)。
当您执行列表推导时, num
值为9
因此下次您遍历vec
。 您将得到9
的清单。
看到这个。
In [1]: vec = [[1,2,3], [4,5,6], [7,8,9]]
In [2]: [num for elem in vec for num in elem]
Out[2]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
In [3]: num
Out[3]: 9
In [4]: [num for elem in vec]
Out[4]: [9, 9, 9]
list1 = [num for elem in vec for num in elem]
#is equivalent to:
list1 = []
for elem in vec:
for num in elem:
list1.append(num)
让我尝试使答案更加明确。 而且顺序显然是从左到右,并且最右边的值将存储在变量中,即num和elem。
初始数据:
vec = [[1,2,3], [4,5,6], [7,8,9]]
num # Undefined
elem # Undefined
步骤1:在执行第[num for elem in vec for num in elem]
# Now the value of undefined variable will be
num = 9 # will keep the last value of the loop as per python 2.7
elem = [7, 8, 9] # Same applies here (as the loop for elem in vec will get executed first followed by for num in elem)
步骤2:执行第[num for elem in vec]
# Output: [9, 9, 9]
# Since num value is 9 and its get repeated 3 times because of vec has 3 elements (three list object in a list, so for loop will run 3 times)
# Now the variable value would be
num = 9 # no change
elem = [7, 8, 9] # The last tuple of variable vec
步骤3:执行[num for elem in (vec for num in elem)]
后执行[num for elem in (vec for num in elem)]
1. In the first/right loop i.e (vec for num in elem)
Here the result will be a generator that will have run thrice since length of elem is 3.
2. Final for loop will iterate over RESULT1 (the result of for loop #1 having length 3) and since the num value is 9. the result will be [9, 9, 9] # num value repeated thrice.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.