ジェネレータの次の要素を返す関数を作成しようとしています。ジェネレータの最後にある場合は、それをリセットして次の結果を返します。以下のコードの出力は、次のようになります。Pythonでジェネレータをリセットするための再帰
1
2
3
1
2
ただし、これは明らかにわかりません。私は何をしているのでしょうか?
a = '123'
def convert_to_generator(iterable):
return (x for x in iterable)
ag = convert_to_generator(a)
def get_next_item(gen, original):
try:
return next(gen)
except StopIteration:
gen = convert_to_generator(original)
get_next_item(gen, original)
for n in range(5):
print(get_next_item(ag,a))
1
2
3
None
None
'convert_to_generator'とは何ですか?そのコードも投稿してください。 – Jarvis
['itertools.cycle'](https://docs.python.org/2/library/itertools.html#itertools.cycle)に興味があるかもしれません。 –
私はそれを@Jarvis – mnky9800n