使用生成器循环遍历列表中的数字

giy*_*21c 1 python generator

每次调用函数时,我都在寻找一种循环遍历列表中数字的方法.

我使用了一个生成器,它一次返回列表一的成员,但是在函数返回最后一个元素之后,我还没有找到回到列表开头的方法.

def returnSeq(self, numRows):
    for seq in [0, 1, 2, 3, 2, 1]:
        yield seq
Run Code Online (Sandbox Code Playgroud)

如果有更好的方法来实现这一点,我将不胜感激.

先感谢您,

Ami*_*ory 8

你基本上是重新实现的itertools.cycle:

import itertools

itertools.cycle([0, 1, 2, 3, 2, 1]) # <- there's your infinite generator
Run Code Online (Sandbox Code Playgroud)