簡體   English   中英

在 Python 中跳過迭代變量的 N 個值的最佳方法是什么?

[英]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 的想法的食譜跳過許多。

itertools.dropwhile()

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM