有一個迭代器,使用list()強制一個真正的list列表, 生成的是[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 對這個迭代器 I 使用next后,再對其使用list()方式生成目錄。 結果不再是[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 迭代器I已經改變了。
>>> R=range(10)>>> list(R)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>> next(R)Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> next(R)TypeError: 'range' object is not an iterator>>> I=iter(R)>>> next(I)0>>> next(I)1>>> list(I)[2, 3, 4, 5, 6, 7, 8, 9]>>> I<range_iterator object at 0x0000018C7DCD2270>>>> next(I)Traceback (most recent call last): File "<pyshell#24>", line 1, in <module> next(I)StopIteration理解這個問題,需要繼續學習!理解透徹后再回來!
新聞熱點
疑難解答