An iterator
The concept of an iteration
#迭代器即迭代的工具, what is iteration?
#迭代是一个重复的过程, the iteration is repeated every time, and the result of each iteration is the initial value of the next iteration while True: #只是单纯地重复, and therefore not the iteration print (' ===> ') l=[1,2,3]count= 0while Count < Len (l): #迭代 print (L[count]) count+=1
Second, why do we have iterators? What is an iterative object? What is an iterator object?
1 #1, why do you have iterators? 2 for sequence types: strings, lists, tuples, we can iterate over the elements that they contain by using an indexed method. But for dictionaries, collections, files, and so on, there is no index, and if you want to take out the elements that are contained inside, you must find an iterative way that does not depend on the index, which is the iterator 3 4 #2, what is an iterative object? 5 An Iterative object refers to an object with a built-in __iter__ method, obj.__iter__, 6 ' hello '. __iter__ 7 (All-in-one). __iter__ 8 [1,2,3].__iter__ 9 {' A ': 1}.__iter__ Ten {' A ', ' B '}.__iter__11 open (' a.txt '). __iter__12 #3, what is an iterator object? 14 Iterative Object Execution obj.__iter__ () results in iterator object 15 whereas the Iterator object refers to an object that has a built-in __iter__ and built-in __next__ method 16 17 file type is an iterator object (' A.txt '). __ ITER__ () Open (' A.txt '). __next__ () #4, note: 23 An Iterator object must be an iterative object, and an iterator object is not necessarily an iterator object
Use of three-iterator objects
1 dic={' A ': 1, ' B ': 2, ' C ': 3} 2 iter_dic=dic.__iter__ () #得到迭代器对象, the iterator object has __iter__ and __next__, but: iterator. __iter__ () The resulting is still the iterator itself 3 iter_dic.__iter__ () is Iter_dic #True 4 5 print (iter_dic.__next__ ()) #等同于next (iter_dic) 6 print (Iter_ Dic.__next__ ()) #等同于next (iter_dic) 7 print (iter_dic.__next__ ()) #等同于next (iter_dic) 8 # Print (iter_dic.__next__ ()) # Throws an exception stopiteration, or the end flag of 9 #有了迭代器, we can not rely on the index iteration to take the value of iter_dic=dic.__iter__ () and 1:13 try:14 k=next ( Iter_dic) (Dic[k]) except stopiteration:17 break18 #这么写太丑陋了, we need to catch the exception ourselves, control next, Python is so cool, can you help me solve it? Can, see for loop
Four for Loop
#基于for循环, we can completely no longer rely on the index to get the value of dic={' a ': 1, ' B ': 2, ' C ': 3}for K in dic: print (dic[k]) #for循环的工作原理 # #: Dic.__iter__ of the object executing in () method to get an iterator object iter_dic#2: Execute Next (iter_dic), assign the resulting value to K, and then execute the Loop body Code # 2: Repeat the process until you catch the exception stopiteration, ending the loop
Advantages and disadvantages of five iterators
#优点: -Provides a unified, index-independent iterative approach -lazy computing, saving memory # Disadvantages: -Unable to get the length (only after the next finish to know how many values)-one -time, can only go backwards, not forward
Two generators
What is a generator
#只要函数内部包含有yield关键字, the result of the function name () is the generator and does not execute the function internal code def func (): print (' ====>first ') yield 1 print (' = ===>second ') yield 2 print (' ====>third ') yield 3 print (' ====>end ') G=func () print (g) #< Generator object Func at 0x0000000002184360>
The second generator is an iterator
G.__iter__g.__next__#2, so the generator is an iterator, so you can take this value res=next (g) print (res)
Good text to the top
12th Day of Python learning