python迭代器和生成器

  • 2020 年 1 月 20 日
  • 筆記

1.經典迭代器

import re    RE_WORD = re.compile('w+')      class Sentence:        def __init__(self, text):          self.text = text          self.words = RE_WORD.findall(text)          def __iter__(self):  # <1>          return SentenceIterator(self.words)  # <2>      class SentenceIterator:        def __init__(self, words):          self.words = words  # <3>          self.index = 0  # <4>        def __next__(self):          try:              word = self.words[self.index]  # <5>          except IndexError:              raise StopIteration()  # <6>          self.index += 1  # <7>          return word  # <8>        def __iter__(self):  # <9>          return self    def main():        s = Sentence('hello all')      for word in s: #隱式調用iter(s),加入s存在__iter__則調用返回迭代器,否則若s存在__getitem__,則默認生成迭代器,調用__getitem__生成元素          print(word)  if __name__ == '__main__':      main()

2.將Sentence中的__iter__改成生成器函數

def __iter__(self):      for word in self.words:      yield word #yield為生成器關鍵字

改成生成器後用法不變,但更加簡潔。

3.惰性實現

當列表比較大,占記憶體較大時,我們可以採用惰性實現,每次只讀取一個元素到記憶體。

def __iter__(self):     for match in RE_WORD.finditer(self.text):        yield match.group() 

或者使用更簡潔的生成器表達式

 def __iter__(self):      return (match.group() for match in RE_WORD.finditer(self.text))

4.yield from

itertools模組含有大量生成器函數可供利用

def chain(*iterables):      for it in iterables:          for i in it:              yield i

等價於

def chain(*iterables):      for it in iterables:          yield from it