1. The concept of iteration
#Iterator is a tool for iteration, so what is iteration? #Iteration is a repeated process, each repetition is an iteration, and the result of each iteration is the initial value of the next iteration while True: # simply repeat, so not iterate print('===>') l=[1,2,3] count=0 while count < len(l): #迭代 print(l[count]) count+=1
2. What is the iterator protocol
#Iterator is a tool for iteration, so what is iteration? #Iteration is a repeated process, each repetition is an iteration, and the result of each iteration is the initial value of the next iteration while True: #It is just a simple repetition, so it is not an iteration print ( ' ===> ' ) l=[1,2,3] count=0 while count < len(l): #迭代 print(l[count]) count+=1 copy code Second, why have iterators? What is an iterable object? What is an iterator object? copy code # 1. Why have iterators? For sequence types: strings, lists, tuples, we can use the index method to iterate and retrieve the elements it contains. But for dictionary, set, file and other types, there is no index. If you want to take out the elements contained in it, you must find an iterative method that does not depend on the index, which is the iterator # 2. What is an iterable object? An iterable object refers to an object with a built-in __iter__ method, namely obj. __iter__ , as follows ' hello ' . __iter__ ( 1,2,3). __iter__ [ 1,2,3]. __iter__ { ' a ' :1 }. __iter__ { ' a ' , ' b ' }. __iter__ open( ' a.txt ' ). __iter__ # 3. What is an iterator object? The result obtained by executing obj .__ iter__ () on an iterable object is the iterator object The iterator object refers to an object that has both built-in __iter__ and built-in __next__ methods file type is an iterator object open('a.txt').__iter__() open('a.txt').__next__() # 4. Note: The iterator object must be an iterable object, and the iterable object is not necessarily an iterator object
three. Use of iterators
dic={'a':1,'b':2,'c':3} iter_dic=dic.__iter__() #Get the iterator object, the iterator object has both __iter__ and __next__, but: iterator.__iter__() still gets the iterator itself iter_dic.__iter__() is iter_dic #True print(iter_dic.__next__()) #equivalent to next(iter_dic) print(iter_dic.__next__()) #equivalent to next(iter_dic) print(iter_dic.__next__()) #equivalent to next(iter_dic) # print(iter_dic.__next__()) #Throw exception StopIteration, or end sign #With iterators, we can iterate over values without relying on the index iter_dic=dic.__iter__() while 1: try: k=next(iter_dic) print (dic [k]) except StopIteration: break #This is too ugly to write, we need to catch exceptions and control next, python is so awesome, can you help me solve it? Yes, see for loop
Four. python for loop
#Based on the for loop, we can no longer rely on the index to get the value dic={'a':1,'b':2,'c':3} for k in dic: print (dic [k])
How #for loops work #1: Execute the dic.__iter__() method of the object after in to get an iterator object iter_dic #2: Execute next(iter_dic), assign the obtained value to k, and then execute the loop body code #3: Repeat process 2 until the exception StopIteration is caught, ending the loop
Advantages and disadvantages of five iterators
#advantage: - Provides a unified, index-independent way of iterating - Lazy computation saves memory #shortcoming: - Unable to get the length (only know how many values there are when the next is finished) - One-time use, can only go backwards, not forwards
generator concept
1. What is a generator
#As long as the function contains the yield keyword, the result of the function name() is a generator, and the code inside the function will not be executed 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>
Two generators are iterators
g.__iter__ g.__next__ #2, so the generator is an iterator, so you can take values like this res=next(g) print(res)