>>> g = (x ** 2 for x in range(10))
>>> next(g)
0
>>> next(g)
1
>>> next(g)
4
>>> next(g)
9
>>> next(g)
16
>>> next(g)
25
>>> next(g)
36
>>> next(g)
49
>>> next(g)
64
>>> next(g)
81
>>> next(g)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

每次调用next,迭代一下迭代器,到最后会触发StopIteration错误。

相关文章:

  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2021-06-02
  • 2021-09-01
  • 2022-12-23
  • 2021-05-20
  • 2021-05-02
猜你喜欢
  • 2022-12-23
  • 2021-07-06
  • 2021-06-28
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
相关资源
相似解决方案