有一个迭代器,使用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
理解这个问题,需要继续学习!理解透彻后再回来!