标签:end generator 特性 高效 eal iter list 数据流 sys
Python 迭代器
来自https://www.runoob.com/python3/python3-iterator-generator.html
来自https://www.liaoxuefeng.com/wiki/1016959663602400/1017323698112640
迭代器的特性:
L = [1, 2, 3, 4] it = iter(L) print(it) # 输出:<list_iterator object at 0x00000000024B9198> print(type(it)) # 输出:<class ‘list_iterator‘> print(next(it)) # 输出:1 print(next(it)) # 输出:2 T = (5, 6, 7, 8) it_1 = iter(T) print(it_1) # 输出:<tuple_iterator object at 0x00000000024B9208> print(type(it_1)) # 输出:<class ‘tuple_iterator‘> print(next(it_1)) # 5 print(next(it_1)) # 6 S = "You‘re really nice" it_2 = iter(S) print(it_2) # 输出:<str_iterator object at 0x00000000024B92E8> print(type(it_2)) # 输出:<class ‘str_iterator‘> print(next(it_2)) # 输出:Y print(next(it_2)) # 输出:o
# 方法1 L = [1, 2, 3, 4] it = iter(L) # 创建迭代器 for x in it: print(x, end=" ") # 输出:1 2 3 4 # 方法2 import sys L2 = [1, 2, 3, 4] it2 = iter(L2) while True: try: print(next(it2)) except StopIteration: sys.exit()
标签:end generator 特性 高效 eal iter list 数据流 sys
原文地址:https://www.cnblogs.com/swordsman180309/p/11779674.html