上海建设三类人员网站,广州的十七做网站,深圳龙华属于宝安区吗,门户网站代码小结 凡是可作用于for循环的对象都是Iterable类型#xff1b; 凡是可作用于next()函数的对象都是Iterator类型#xff0c;它们表示一个惰性计算的序列#xff1b; 集合数据类型如list、dict、str等是Iterable但不是Iterator#xff0c;不过可以通过iter()函数获得一个Itera…小结 凡是可作用于for循环的对象都是Iterable类型 凡是可作用于next()函数的对象都是Iterator类型它们表示一个惰性计算的序列 集合数据类型如list、dict、str等是Iterable但不是Iterator不过可以通过iter()函数获得一个Iterator对象。 Python的for循环本质上就是通过不断调用next()函数实现的例如 for x in [1, 2, 3, 4, 5]: pass 实际上完全等价于 # 首先获得Iterator对象:
it iter([1, 2, 3, 4, 5]) # 循环: while True: try: # 获得下一个值: x next(it) except StopIteration: # 遇到StopIteration就退出循环 break转载于:https://www.cnblogs.com/zang963469010/p/5994253.html