迭代器例子
迭代器例子
li=[1,2,3,4]
a=iter(li) #iter(li)将列表变成迭代器,迭代器赋值给a
print(next(a))
print(next(a))
print(next(a))
print(next(a))
print(next(a))
生成器例子
def fun():
i=0
while i<5:
yield'暂停'#添加yield变成一个生成器,运行到yield就结束
i+=1
print(i)
a=fun()
print(a)
print(next(a))
print(next(a))
print(next(a))
print(next(a))