[英]What's the best way of skip N values of the iteration variable in Python?
In many languages we can do something like:在许多语言中,我们可以执行以下操作:
for (int i = 0; i < value; i++)
{
if (condition)
{
i += 10;
}
}
How can I do the same in Python?我如何在 Python 中做同样的事情? The following (of course) does not work:
以下(当然)不起作用:
for i in xrange(value):
if condition:
i += 10
I could do something like this:我可以做这样的事情:
i = 0
while i < value:
if condition:
i += 10
i += 1
but I'm wondering if there is a more elegant ( pythonic? ) way of doing this in Python.但我想知道在 Python 中是否有更优雅的( pythonic? )方式来做到这一点。
Use continue
.使用
continue
。
for i in xrange(value):
if condition:
continue
If you want to force your iterable to skip forwards, you must call .next()
.如果你想强制你的迭代向前跳过,你必须调用
.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]
As you can see, this is disgusting.如您所见,这很恶心。
Create the iterable before the loop.在循环之前创建可迭代对象。
Skip one by using next on the iterator通过在迭代器上使用 next 跳过一个
it = iter(xrange(value))
for i in it:
if condition:
i = next(it)
Skip many by using itertools or recipes based on ideas from itertools.通过使用 itertools 或基于 itertools 的想法的食谱跳过许多。
itertools.dropwhile() itertools.dropwhile()
it = iter(xrange(value))
for i in it:
if x<5:
i = dropwhile(lambda x: x<5, it)
Take a read through the itertools page, it shows some very common uses of working with iterators.阅读 itertools 页面,它显示了使用迭代器的一些非常常见的用法。
itertools islice itertools islice
it = islice(xrange(value), 10)
for i in it:
...do stuff with i...
Itertools has a recommended way to do this: https://docs.python.org/3.7/library/itertools.html#itertools-recipes 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))
Now you can do:现在你可以这样做:
for i in tail(5, range(10)):
print(i)
to get要得到
5
6
7
8
9
It's a very old question, but I find the accepted answer is not totally stisfactory:这是一个非常古老的问题,但我发现接受的答案并不完全令人满意:
if ... / [next()...]
sequence, the value of i
hasn't changed.if ... / [next()...]
序列之后, i
的值没有改变。 In your first example, it has. Using a modified version of consume
in itertools recipes , you can write:在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)
As written in the doctstring of consume
, the iterator is consumed at C speed (didn't benchmark though).正如
consume
的文档字符串中所写,迭代器以 C 速度消耗(虽然没有进行基准测试)。 Output:输出:
0->5
6->6
7->7
8->13
14->14
15->15
16->None
With a minor modification, one can get 21
instead of None
, but I think this isnot a good idea because this code does work with any iterable (otherwise one would prefer the while
version):稍作修改,就可以获得
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)
Output:输出:
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
I am hoping I am not answering this wrong... but this is the simplest way I have come across:我希望我没有回答这个错误......但这是我遇到的最简单的方法:
for x in range(0,10,2):
print x
output should be something like this:输出应该是这样的:
0
2
4
6
8
The 2 in the range parameter's is the jump value range参数中的2是跳转值
There are a few ways to create iterators , but the custom iterator class is the most extensible:有几种方法可以创建迭代器,但自定义迭代器类是最可扩展的:
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
and in use:并在使用中:
>>> 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,
Does a generator function here is rebundant?这里的生成器函数是否是冗余的? Like this:
像这样:
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.