跳过循环中的多个迭代 [英] Skip multiple iterations in loop

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

问题描述

我有一个循环列表,我想在达到look之后跳过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

四次continue当然是胡说八道,而四次使用next()是行不通的.

Four times continue is nonsense of course and using four times next() doesn't work.

输出应如下所示:

always
look
aside
of
life

推荐答案

for使用iter(song)进行循环;您可以在自己的代码中执行此操作,然后在循环内推进迭代器;再次在iterable上调用iter()只会返回相同的可迭代对象,因此您可以在下一次迭代中紧随其后的for在循环内推进iterable.

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()函数推进迭代器;它可以在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)迭代器将跳过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

这篇关于跳过循环中的多个迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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