python 跳过迭代,在循环python中跳过多次迭代

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值