Discuss / Python / 生成器:(吝啬鬼)

生成器:(吝啬鬼)

Topic source

樊睡懒觉

#1 Created at ... [Delete] [Delete and Lock User]
  1. 意义:生成器:generator要一个吐一个,节约空间
  2. 生成: 1.将列表生成式变成(): g = (x * x for x in range(10)) 2.函数中print(b) 改为 yield b
  3. 使用: 1.next(g) 2.for n in g:
  4. 差异:函数以return或者最后一行为结点,生成器以yield为节点
  5. 例子:数列、杨辉三角
  6. 新知:StopIteration、捕获错误

  • 1

Reply