Python_迭代器
迭代器:迭代器里的元素读一个丢一个,不能回退,不能用下标访问
x.__next__():迭代器里唯一的方法,只读下一个
d = iter(['Presly', 'is', 'lovely', ]) print(d.__next__()) print(d.__next__()) print(d.__next__()) #print(d.__next__()) # 迭代器里的元素已经被读完,继续直接报错
结果:
Presly is lovely
迭代器:迭代器里的元素读一个丢一个,不能回退,不能用下标访问
x.__next__():迭代器里唯一的方法,只读下一个
d = iter(['Presly', 'is', 'lovely', ]) print(d.__next__()) print(d.__next__()) print(d.__next__()) #print(d.__next__()) # 迭代器里的元素已经被读完,继续直接报错
结果:
Presly is lovely