在许多语言中,我们可以做类似的事情:
for (int i = 0; i < value; i++)
{
if (condition)
{
i += 10;
}
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在Python中做同样的事情?以下(当然)不起作用:
for i in xrange(value):
if condition:
i += 10
Run Code Online (Sandbox Code Playgroud)
我可以这样做:
i = 0
while i < value:
if condition:
i += 10
i += 1
Run Code Online (Sandbox Code Playgroud)
但我想知道在Python中是否有更优雅(pythonic?)的方式.
bra*_*ers 26
使用continue.
for i in xrange(value):
if condition:
continue
Run Code Online (Sandbox Code Playgroud)
如果你想强制你的iterable向前跳过,你必须打电话.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]
Run Code Online (Sandbox Code Playgroud)
如你所见,这很恶心.
kev*_*pie 17
在循环之前创建iterable.
在迭代器上使用next跳过一个
it = iter(xrange(value))
for i in it:
if condition:
i = next(it)
Run Code Online (Sandbox Code Playgroud)
通过使用基于itertools的想法的itertools或配方来跳过许多.
it = iter(xrange(value))
for i in it:
if x<5:
i = dropwhile(lambda x: x<5, it)
Run Code Online (Sandbox Code Playgroud)
阅读itertools页面,它显示了使用迭代器的一些非常常见的用法.
itertools islice
it = islice(xrange(value), 10)
for i in it:
...do stuff with i...
Run Code Online (Sandbox Code Playgroud)
这是一个非常古老的问题,但我发现接受的答案并不完全令人满意:
if ... / [next()...]序列之后, 的值i没有改变。在你的第一个例子中,它有。consume使用itertools Recipes的修改版本,您可以编写:
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)
Run Code Online (Sandbox Code Playgroud)
正如 的文档字符串中所写consume,迭代器以 C 速度消耗(尽管没有进行基准测试)。输出:
0->5
6->6
7->7
8->13
14->14
15->15
16->None
Run Code Online (Sandbox Code Playgroud)
通过稍加修改,就可以代替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)
Run Code Online (Sandbox Code Playgroud)
输出:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28145 次 |
| 最近记录: |