#迭代器? 有iter方法和next方法 # l = [1,2,3,4,5] # i = iter(l) #<list_iterator object at 0x000001F0156DE088> 生成迭代器 # print(i) # print(next(i)) #1 # print(next(i)) #2 # print(next(i)) #3 # print(next(i)) #4 # print(next(i)) #5 # lo = [1, 2, 3, 4, 5, 6] # for y in lo: # ci = iter(lo) #列表转换成迭代器 # print(next(ci)) #还不能用 it = iter([1, 2, 3, 4, 5]) # 循环: while True: try: # 获得下一个值: x = next(it) print(x) except StopIteration: # 遇到StopIteration就退出循环 break