迭代器
1.什么是迭代器?
迭代器:
能被next()函数调用并不断返回下一个值的对象称为迭代器(Iterator 迭代器是对象)
概念:
迭代器指的是迭代取值的工具,迭代是一个重复的过程,每次重复都是基于上一次的结果而继续的,
单纯的重复并不是迭代
特征:
不依赖索引,而通过next指针(内存地址寻址)迭代所有数据,一次只取一个值,
而不是一股脑的把所有数据放进内存.大大节省空间,
2.可迭代对象
setvar = {"leon","mary","tom","bear"} # 获取当前对象的内置成员 lst = dir(setvar) print(lst) 结果: ['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update'] # 判断是否是可迭代对象 res = "__iter__" in lst print(res) # for i in setvar: # print(i) 结果: True leon
mary
tom
bear
3.迭代器
for循环之所以可以遍历所有的数据,是因为底层使用了迭代器,通过地址寻址的方式,一个一个的找数据;
可迭代对象 -> 迭代器 实际上就是从不能够被next直接调用 -> 可以被next指针直接调用的过程
如果是可迭代对象 -> 不一定是迭代器
如果是迭代器 -> 一定是可迭代对象
如何创建一个迭代器?
1 setvar = {"李东强","高英培","刘吉昌","郭二河"} 2 it = iter(setvar) 3 print(it)
结果:
<set_iterator object at 0x000001926E7490D8>
如何判断一个迭代器? print(dir(it)) res = "__iter__" in dir(it) and "__next__" in dir(it) print(res)
结果:
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__length_hint__', '__lt__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__']
True
如何调用一个迭代器 """next是单向不可逆的过程,一条路走到黑""" res = next(it) print(res) res = next(it) print(res) res = next(it) print(res) res = next(it) print(res) # res = next(it) # print(res)
1 # 4.重置迭代器 2 it = iter(setvar) 3 print( it.__next__() ) 4 print( it.__next__() ) 5 print( it.__next__() ) 6 print( it.__next__() ) 7 8 # 5.调用迭代器的其他方法 9 # 1 for 10 it = iter(setvar) 11 for i in it: 12 print(i) 13 14 print("<======>") 15 # 2 for + next 16 it = iter(setvar) 17 for i in range(2): 18 print( next(it) ) 19 20 print( next(it) ) 21 print( next(it) ) 22 # print( next(it) ) error 超出了寻址范围 23 24 # 6.判断迭代器/可迭代对象的其他方法 25 # 从...模块 引入...内容 26 from collections import Iterator, Iterable 27 """Iterator 迭代器 Iterable 可迭代的对象""" 28 res = isinstance(it,Iterator) 29 print(res) 30 res = isinstance(it,Iterable) 31 print(res) 32 33 # 7.range是迭代器么? 34 print(isinstance(range(10),Iterator)) # False 35 print(isinstance(range(10),Iterable)) # True 36 37 # 变成迭代器 38 it = range(10).__iter__() 39 print(isinstance(it,Iterator)) # True 40 print(isinstance(it,Iterable)) # True 41 42 # 调用it 43 # next 44 res = next(it) 45 print(res) 46 res = next(it) 47 print(res) 48 49 print("<=====>") 50 # for + next 51 for i in range(3): 52 print(next(it)) 53 54 print("<=====>") 55 # for 56 for i in it: 57 print(i)