>>> 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錯誤。
以上是python迭代器中next()的用法範例的詳細內容。更多資訊請關注PHP中文網其他相關文章!