![見出し画像](https://assets.st-note.com/production/uploads/images/30661250/rectangle_large_type_2_4d6642960e8eaeb59f2396f40c0b9772.png?width=1200)
pythonの小技集-ジェネレーターの使い方-
ジェネレーターは戻り値をyieldで返します。
nextで順番に取り出します。
コード
def get_str(str):
for s in str:
yield s
str = 'あいうえお'
a = get_str(str)
b = next(a)
c = next(a)
print(b)
出力結果
あ
い
この記事が気に入ったらサポートをしてみませんか?
ジェネレーターは戻り値をyieldで返します。
nextで順番に取り出します。
def get_str(str):
for s in str:
yield s
str = 'あいうえお'
a = get_str(str)
b = next(a)
c = next(a)
print(b)
あ
い
この記事が気に入ったらサポートをしてみませんか?