理解Python中的列表理解

时间:2016-07-16 07:42:51

标签: python python-2.7 list-comprehension

在阅读official tutorial时,我遇到了这个例子:

>>> 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]
>>> 

4 个答案:

答案 0 :(得分:4)

列表推导中的循环从左到右读取。如果您的列表理解将被写为普通循环,它将看起来像这样:

>>> 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

答案 1 :(得分:2)

不用担心:)。

当您执行列表理解时,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]

答案 2 :(得分:0)

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)

答案 3 :(得分:0)

让我试着让答案更加清晰。 顺序显然是从左到右,最正确的值将存储在变量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)]

之后
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.