迭代器:
list = [1,2,3,4] it = iter(list) print(next(it)) print(next(it)) print(next(it)) print(next(it)) print("------------") list = [1,2,3,4] it = iter(list) for i in it: print(i)
生成器:
def f(): a = 5 while a: yield a a -= 1 k = f() #k是一个迭代器,由生成器返回生成 for i in k: print(i)