タグ

Pythonとgeneratorに関するarveltのブックマーク (2)

  • What does the "yield" keyword do in Python?

    To understand what yield does, you must understand what generators are. And before you can understand generators, you must understand iterables. Iterables When you create a list, you can read its items one by one. Reading its items one by one is called iteration: >>> mylist = [1, 2, 3] >>> for i in mylist: ... print(i) 1 2 3 mylist is an iterable. When you use a list comprehension, you create a li

    What does the "yield" keyword do in Python?
  • Pythonのイテレータとジェネレータ - Qiita

    Pythonのイテレータ(iterator)とジェネレータ(generator)についてまとめてみます。 (追記2018.12.25: Python3の文法に全面的に置き換えました) イテレータ: 要素を反復して取り出すことのできるインタフェース ジェネレータ: イテレータの一種であり、1要素を取り出そうとする度に処理を行い、要素をジェネレートするタイプのもの。Pythonではyield文を使った実装を指すことが多いと思われる Python組み込みのコレクション(list, tuple, set, dictなど)はどれもイテレーション可能ですが、組み込みのコレクションを使った繰り返し処理ではあらかじめコレクションに値を入れておく必要があるため、以下のようなケースではイテレータやジェネレータを自分で実装したいというケースがあると思います。 無限に繰り返すイテレーション 要素すべてをあらかじめ

    Pythonのイテレータとジェネレータ - Qiita
  • 1