迭代器

#迭代器? 有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


it = [1, 2, 3, 4, 5]
cc = iter(it)
for i in range(5):
    ccc = next(cc)
    print(ccc)




it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
    try:
        # 获得下一个值:
        x = next(it)
        print(x)
    except StopIteration:
        # 遇到StopIteration就退出循环
        break
原文地址:https://www.cnblogs.com/TKOPython/p/11755470.html