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

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

问题描述

我有一个循环列表,我想在达到 look 后跳过 3 个元素.在这个答案 提出了一些建议,但我没有很好地利用它们:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']合唱:如果唱 == '看':印唱继续继续继续继续打印 'a' + 唱印唱

四次continue当然是无稽之谈,使用四次next()不起作用.

输出应如下所示:

总是看在旁边的生活

解决方案

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

使用 next() 函数推进迭代器;它在 Python 2 和 3 中都可以正常工作,而无需调整语法:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']Song_iter = iter(歌曲)在 Song_iter 中唱歌:印唱如果唱 == '看':下一个(song_iter)下一个(song_iter)下一个(song_iter)打印 'a' + next(song_iter)

通过移动 print sing 行,我们也可以避免重复自己.

使用 next() 这种方式可以引发 StopIteration 异常,如果可迭代对象没有值.

您可以捕获该异常,但给 next() 提供第二个参数会更容易,默认值可以忽略异常并返回默认值:

song = ['always', 'look', 'on', 'the', 'bright', 'side', 'of', 'life']Song_iter = iter(歌曲)在 Song_iter 中唱歌:印唱如果唱 == '看':下一个(song_iter,无)下一个(song_iter,无)下一个(song_iter,无)打印 'a' + next(song_iter, '')

我会使用 itertools.islice() 改为跳过 3 个元素;保存重复的 next() 调用:

from itertools import islice歌曲 = ['总是', '看', 'on', 'the', 'bright', 'side', 'of', 'life']Song_iter = iter(歌曲)在 Song_iter 中唱歌:印唱如果唱 == '看':打印 'a' + next(islice(song_iter, 3, 4), '')

islice(song_iter, 3, 4) 迭代将跳过 3 个元素,然后返回第 4 个元素,然后完成.对该对象调用 next() 从而从 song_iter() 中检索第四个元素.

演示:

<预><代码>>>>从 itertools 导入 islice>>>歌曲 = ['总是', '看', 'on', 'the', 'bright', 'side', 'of', 'life']>>>Song_iter = iter(歌曲)>>>在 Song_iter 中唱歌:...打印唱歌...如果唱 == '看':... 打印 'a' + next(islice(song_iter, 3, 4), '')...总是看在旁边的生活

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

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

The output should look like:

always
look
aside
of
life

解决方案

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.

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)

By moving the print sing line up we can avoid repeating ourselves too.

Using next() this way can raise a StopIteration exception, if the iterable is out of values.

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, '')

I'd use itertools.islice() to skip 3 elements instead; saves repeated next() calls:

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), '')

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().

Demo:

>>> 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天全站免登陆