Python列表理解循环 [英] Python list comprehension for loops

查看:91
本文介绍了Python列表理解循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Python Wikibook ,对这部分感到困惑:

I'm reading the Python wikibook and feel confused about this part:

列表理解支持多个for语句.它会 依次评估所有对象中的项目,并将循环 如果一个对象比其他对象长,则在较短的对象上覆盖.

List comprehension supports more than one for statement. It will evaluate the items in all of the objects sequentially and will loop over the shorter objects if one object is longer than the rest.

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

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

I understand the usage of nested for loops but I don't get

...并会循环 如果一个对象长于其余对象,则覆盖较短的对象

...and will loop over the shorter objects if one object is longer than the rest

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

What does this mean? (shorter, longer...)

推荐答案

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

These type of nested loops create a Cartesian Product of the two sequences. Try it:

>>> [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':相同:

The inner 'x' in the list comprehension above (ie, the for x in 'cat' part) the is the same as the outer for x in 'cat': in this example:

>>> 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"循环变长相同:

So the effect of making one shorter or longer is the same as making the 'x' or 'y' loop longer in two nested loops:

>>> 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不同,在配对过程中,配对将在较短序列的结尾处终止.

In each case, the shorter sequence is looped over again until the longer sequence is exhausted. This unlike zip where the pairing would be terminated at the end of the shorter sequence.

关于嵌套循环与zip的混淆(在注释中).

There seems to be confusion (in the comments) about nested loops versus zip.

嵌套循环:

如上所示,这:

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

与两个嵌套的"for"循环相同,外部循环为"x".

is the same as two nested 'for' loops with 'x' the outer loop.

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

Nested loops will execute the inner y loop the range of x in the outer loop times.

所以:

>>> [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中的产品可以获得相同的结果:

You can get the same result with product from 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 is similar but stops after the shorter sequence is exhausted:

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

您可以将 itertools 用于将要压缩的拉链直到用尽了最长的序列,但结果却不同:

You can use itertools for a zip that will zip until the longest sequence is exhausted, but the result is different:

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

这篇关于Python列表理解循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆