在循环python中跳过多次迭代 [英] Skip multiple iterations in loop python
问题描述
我在循环中有一个列表,我想在看
之后跳过3个元素。
在这个答案提出了一些建议,但我没有很好地利用它们:
I have a list in a loop and I want to skip 3 elements after look
has been reached.
In this answer a couple of suggestions were made but I fail to make good use of them:
song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
for sing in song:
if sing == 'look':
print sing
continue
continue
continue
continue
print 'a' + sing
print sing
四次继续
当然是无稽之谈,使用四次 next()
不起作用。
Four times continue
is nonsense of course and using four times next()
doesn't work.
输出应如下所示:
always
look
aside
of
life
推荐答案
for
使用 iter(歌曲)
循环;你可以在自己的代码中执行此操作,然后在循环内推进迭代器;再次在iterable上调用 iter()
将只返回相同的可迭代对象,因此您可以使用为$ c $提升循环内的iterable c>在下一次迭代中跟随。
for
uses iter(song)
to loop; you can do this in your own code and then advance the iterator inside the loop; calling iter()
on the iterable again will only return the same iterable object so you can advance the iterable inside the loop with for
following right along in the next iteration.
使用 next()
function ;它在Python 2和3中都能正常工作而无需调整语法:
Advance the iterator with the next()
function; it works correctly in both Python 2 and 3 without having to adjust syntax:
song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
print sing
if sing == 'look':
next(song_iter)
next(song_iter)
next(song_iter)
print 'a' + next(song_iter)
通过移动 print sing
排队,我们可以避免重复自己。
By moving the print sing
line up we can avoid repeating ourselves too.
使用 next()
这样可以提出 StopIteration
异常,如果可迭代超出值。
Using next()
this way can raise a StopIteration
exception, if the iterable is out of values.
你可以捕获该异常,但它会更容易给 next()
第二个参数,一个默认值来忽略异常并返回默认值:
You could catch that exception, but it'd be easier to give next()
a second argument, a default value to ignore the exception and return the default instead:
song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
print sing
if sing == 'look':
next(song_iter, None)
next(song_iter, None)
next(song_iter, None)
print 'a' + next(song_iter, '')
我使用 itertools.islice()
代替跳过3个元素;保存重复 next()
调用:
from itertools import islice
song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
song_iter = iter(song)
for sing in song_iter:
print sing
if sing == 'look':
print 'a' + next(islice(song_iter, 3, 4), '')
islice(song_iter,3,4)
iterable将跳过3个元素,然后返回第4个元素,然后完成。在该对象上调用 next()
,从而从 song_iter()
中检索第4个元素。
The islice(song_iter, 3, 4)
iterable will skip 3 elements, then return the 4th, then be done. Calling next()
on that object thus retrieves the 4th element from song_iter()
.
演示:
>>> from itertools import islice
>>> song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']
>>> song_iter = iter(song)
>>> for sing in song_iter:
... print sing
... if sing == 'look':
... print 'a' + next(islice(song_iter, 3, 4), '')
...
always
look
aside
of
life
这篇关于在循环python中跳过多次迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!