繁体   English   中英

循环的Python列表理解

[英]Python list comprehension for loops

我正在阅读Python wikibook并对此部分感到困惑:

List comprehension支持多个for语句。 它将按顺序评估所有对象中的项目,如果一个对象比其余对象长,则将遍历较短的对象。

 >>>item = [x+y for x in 'cat' for y in 'pot'] >>>print item ['cp', 'co', 'ct', 'ap', 'ao', 'at', 'tp', 'to', 'tt'] 

我理解嵌套for循环的用法,但我不明白

...如果一个对象比其余对象长,则会遍历较短的对象

这是什么意思? (更短,更长......)

这些类型的嵌套循环创建两个序列的笛卡尔积 试试吧:

>>> [x+y for x in 'cat' for y in 'potty']
['cp', 'co', 'ct', 'ct', 'cy', 'ap', 'ao', 'at', 'at', 'ay', 'tp', 'to', 'tt', 'tt', 'ty']
>>> [x+y for x in 'catty' for y in 'pot']
['cp', 'co', 'ct', 'ap', 'ao', 'at', 'tp', 'to', 'tt', 'tp', 'to', 'tt', 'yp', 'yo', 'yt']

上面列表理解中的内部 'x'(即for x in 'cat'部分中的for x in 'cat':外部相同for x in 'cat':在此示例中:

>>> li=[]
>>> for x in 'cat':
...    for y in 'pot':
...       li.append(x+y)
# li=['cp', 'co', 'ct', 'ap', 'ao', 'at', 'tp', 'to', 'tt']

因此,使一个更短或更长的效果与在两个嵌套循环中使'x'或'y'循环更长时间相同:

>>> li=[]
>>> for x in 'catty':
...    for y in 'pot':
...       li.append(x+y)
... 
>>> li==[x+y for x in 'catty' for y in 'pot']
True

在每种情况下,较短的序列再次循环,直到较长的序列耗尽。 这与zip不同,其中配对将在较短序列的末尾终止。

编辑

有关嵌套循环与zip的混淆(在评论中)似乎有些混乱。

嵌套循环:

如上所示,这个:

[x+y for x in '12345' for y in 'abc']

与两个嵌套的'for'循环相同,其中'x'是外循环。

嵌套循环将在外循环时间内执行内部y循环x的范围。

所以:

>>> [x+y for x in '12345' for y in 'ab']
    ['1a', '1b',   # '1' in the x loop
     '2a', '2b',   # '2' in the x loop, b in the y loop
     '3a', '3b',   # '3' in the x loop, back to 'a' in the y loop
     '4a', '4b',   # so on
     '5a', '5b'] 

您可以使用itertools的产品获得相同的结果:

>>> from itertools import product
>>> [x+y for x,y in product('12345','ab')]
['1a', '1b', '2a', '2b', '3a', '3b', '4a', '4b', '5a', '5b']

Zip 类似但在较短的序列耗尽后停止:

>>> [x+y for x,y in zip('12345','ab')]
['1a', '2b']
>>> [x+y for x,y in zip('ab', '12345')]
['a1', 'b2']

您可以使用itertools进行压缩,直到最长的序列耗尽,但结果不同:

>>> import itertools
>>> [x+y for x,y in itertools.zip_longest('12345','ab',fillvalue='*')]
['1a', '2b', '3*', '4*', '5*'] 

好吧,Python文档没有谈到任何这样的短/长案例: http//docs.python.org/2/tutorial/datastructures.html#list-comprehensions 在列表理解中具有两个“for”意味着具有两个循环。 @drewk指出的例子是正确的。

让我复制它是为了解释:

>>> [x+y for x in '123' for y in 'pot']
['1p', '1o', '1t', '2p', '2o', '2t', '3p', '3o', '3t']
>>>
>>> [x+y for x in '1' for y in 'pot']
['1p', '1o', '1t']
>>>

在这两种情况下,第一个“for”形成外部循环,而第二个“for”形成内部循环。 那是这里唯一的不变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM