跳回python迭代器

跳回python迭代器,python,python-3.x,for-loop,Python,Python 3.x,For Loop,像这样的C代码,最有效的方法是什么 for(int i = 0; i < 10; i++) { printf("%d\n", i); if(i == 5) i = 1; } 我知道在next()的范围内有 因此,此代码将每秒打印一个数字,但我不知道如何跳回for循环。您可以使用永久循环,并可以使用附加开始条件,如: import itertools as it for i in it.chain((0,), it.cycle(range(1, 6))):

像这样的C代码,最有效的方法是什么

for(int i = 0; i < 10; i++) {
    printf("%d\n", i);
    if(i == 5)
        i = 1;
}
我知道在next()的范围内有


因此,此代码将每秒打印一个数字,但我不知道如何跳回for循环。

您可以使用永久循环,并可以使用附加开始条件,如:

import itertools as it
for i in it.chain((0,), it.cycle(range(1, 6))):
    print(i)
结果:
@斯蒂芬:我尝试不用锁链,效果很好。这是很好的代码

import itertools as it
for i in it.cycle(range(1, 6)):
    print(i)

因此,我从评论中意识到,我必须以这样的方式使用while循环

i = 0
while i < 10:
    print(i)
    if i == 5:
        i = 1
i=0
当我<10时:
印刷品(一)
如果i==5:
i=1

上面关于itertools和chain的例子很好,但是我想要一些类似while cycle的解决方案,但是我没有考虑while。在我的代码中,有一些更复杂的条件,不仅是这个简单的“链”,而且我认为它对于“跳转”的演示已经足够了。

您可以使用迭代器类重现大部分C风格的for循环

例如:

class cFor:   
    def __init__(self,start,condition,step=1):
        self.index     = start
        self.nextIndex = start
        self.doLoop    = condition
        if type(condition) == type(start):
            self.doLoop  = lambda x:x < condition
        self.getNext   = step
        if type(step) == type(start):
            self.getNext = lambda x:x + step

    def __iter__(self):
        return self

    def next(self) : return self.__next__()

    def __next__(self):
        if self.doLoop(self.nextIndex):
            self.index     = self.nextIndex
            self.nextIndex = self.getNext(self.index)
            return self.index
        raise StopIteration()
并且,使用lambdas,您可以提供自己的条件和增量计算:

for i in cFor(1, lambda i:i<=10, lambda i:i+3): print(i)
# 1, 4, 7, 10
例如,您可以使用它实现一个简单的冒泡排序:

arr = [3,7,4,2,8,1,6]

for i,loop in cForLoop(1,len(arr)):
    if arr[i-1] > arr[i] :
        arr[i],arr[i-1] = arr[i-1],arr[i]
        loop.nextIndex = max(1,i-1) 

# arr == [1, 2, 3, 4, 6, 7, 8]

你不能倒带迭代器。为什么要调用
next()
?for循环自动为您调用next()。为什么不使用while循环呢?我不确定我是否理解,但有很多方法?您可以使用一个while循环,使用True并将i增加1,然后将其重置为5。您可以在Python中编写完全相同的C代码,tooThis是此代码的副本。在其他新闻中,我如何报告某个内容是重复的?最后,为了回答这个问题,我将使用while循环。您也可以从所需位置中断并再次开始迭代。@TravisBlack单击
标志
,然后选择“复制”。@OP,由于此代码将无限期运行,您可以在for循环中设置单独的中断条件。谢谢,但这是一个非常具体的示例,我想要更一般的,喜欢设置迭代器的实际值,但是谢谢。
class cFor:   
    def __init__(self,start,condition,step=1):
        self.index     = start
        self.nextIndex = start
        self.doLoop    = condition
        if type(condition) == type(start):
            self.doLoop  = lambda x:x < condition
        self.getNext   = step
        if type(step) == type(start):
            self.getNext = lambda x:x + step

    def __iter__(self):
        return self

    def next(self) : return self.__next__()

    def __next__(self):
        if self.doLoop(self.nextIndex):
            self.index     = self.nextIndex
            self.nextIndex = self.getNext(self.index)
            return self.index
        raise StopIteration()
for i in cFor(0,10,2) : print(i)
# 0, 2, 4, 6, 8
for i in cFor(1, lambda i:i<=10, lambda i:i+3): print(i)
# 1, 4, 7, 10
[ a for a,b in cFor((1,1),lambda ab:ab[1]<30,lambda ab:(ab[1],sum(ab))) ]
# [ 1, 1, 2, 3, 5, 8, 13 ]
def cForLoop(start,condition,step=1):
    loop = cFor(start,condition,step)
    while True : yield loop.next(), loop

for i,loop in cForLoop(0,10):
    print(i)
    if i == 5 : loop.nextIndex = 1
# 0, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, ....
arr = [3,7,4,2,8,1,6]

for i,loop in cForLoop(1,len(arr)):
    if arr[i-1] > arr[i] :
        arr[i],arr[i-1] = arr[i-1],arr[i]
        loop.nextIndex = max(1,i-1) 

# arr == [1, 2, 3, 4, 6, 7, 8]