【未解决】next是什么?会在原地改变对象?

有一个迭代器,使用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

理解这个问题,需要继续学习!理解透彻后再回来!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值