[英]What's the best way of skip N values of the iteration variable in Python?
在许多语言中,我们可以执行以下操作:
for (int i = 0; i < value; i++)
{
if (condition)
{
i += 10;
}
}
我如何在 Python 中做同样的事情? 以下(当然)不起作用:
for i in xrange(value):
if condition:
i += 10
我可以做这样的事情:
i = 0
while i < value:
if condition:
i += 10
i += 1
但我想知道在 Python 中是否有更优雅的( pythonic? )方式来做到这一点。
使用continue
。
for i in xrange(value):
if condition:
continue
如果你想强制你的迭代向前跳过,你必须调用.next()
。
>>> iterable = iter(xrange(100))
>>> for i in iterable:
... if i % 10 == 0:
... [iterable.next() for x in range(10)]
...
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50]
[61, 62, 63, 64, 65, 66, 67, 68, 69, 70]
[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
如您所见,这很恶心。
在循环之前创建可迭代对象。
通过在迭代器上使用 next 跳过一个
it = iter(xrange(value))
for i in it:
if condition:
i = next(it)
通过使用 itertools 或基于 itertools 的想法的食谱跳过许多。
it = iter(xrange(value))
for i in it:
if x<5:
i = dropwhile(lambda x: x<5, it)
阅读 itertools 页面,它显示了使用迭代器的一些非常常见的用法。
itertools islice
it = islice(xrange(value), 10)
for i in it:
...do stuff with i...
Itertools 有一个推荐的方法来做到这一点: https ://docs.python.org/3.7/library/itertools.html#itertools-recipes
import collections
def tail(n, iterable):
"Return an iterator over the last n items"
# tail(3, 'ABCDEFG') --> E F G
return iter(collections.deque(iterable, maxlen=n))
现在你可以这样做:
for i in tail(5, range(10)):
print(i)
要得到
5
6
7
8
9
这是一个非常古老的问题,但我发现接受的答案并不完全令人满意:
if ... / [next()...]
序列之后, i
的值没有改变。 在你的第一个例子中,它有。 在itertools recipes 中使用修改版本的consume
,您可以编写:
import itertools
def consume(it, n):
return next(itertools.islice(it, n-1, n), None)
it = iter(range(20))
for i in it:
print(i, end='->')
if i%4 == 0:
i = consume(it, 5)
print(i)
正如consume
的文档字符串中所写,迭代器以 C 速度消耗(虽然没有进行基准测试)。 输出:
0->5
6->6
7->7
8->13
14->14
15->15
16->None
稍作修改,就可以获得21
而不是None
,但我认为这不是一个好主意,因为此代码确实适用于任何可迭代对象(否则人们会更喜欢while
版本):
import string
it = iter(string.ascii_lowercase) # a-z
for x in it:
print(x, end="->")
if x in set('aeiouy'):
x = consume(it, 2) # skip the two letters after the vowel
print(x)
输出:
a->c
d->d
e->g
h->h
i->k
l->l
m->m
n->n
o->q
r->r
s->s
t->t
u->w
x->x
y->None
我希望我没有回答这个错误......但这是我遇到的最简单的方法:
for x in range(0,10,2):
print x
输出应该是这样的:
0
2
4
6
8
range参数中的2是跳转值
class skip_if: # skip_if(object) for python2
"""
iterates through iterable, calling skipper with each value
if skipper returns a positive integer, that many values are
skipped
"""
def __init__(self, iterable, skipper):
self.it = iter(iterable)
self.skip = skipper
def __iter__(self):
return self
def __next__(self): # def next(self): for python2
value = next(self.it)
for _ in range(self.skip(value)):
next(self.it, None)
return value
并在使用中:
>>> for i in skip_if(range(1,100), lambda n: 10 if not n%10 else 0):
... print(i, end=', ')
...
1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
61, 62, 63, 64, 65, 66, 67, 68, 69, 70,
81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
这里的生成器函数是否是冗余的? 像这样:
def filterRange(range, condition):
x = 0
while x < range:
x = (x+10) if condition(x) else (x + 1)
yield x
if __name__ == "__main__":
for i in filterRange(100, lambda x: x > 2):
print i
我认为你必须为此使用一个 while 循环...... for 循环在一个可迭代对象上循环......并且你不能像你想在这里做的那样跳过下一个项目
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.