Full-stack Python development: python iterator, generator, and python Generator
Iterator 1. What is iterator?
# The iterator is an iteration tool. What is iteration? # Iteration is a repetitive process. Each iteration is an iteration, and the result of each iteration is the initial value of the next iteration while True: # It is simply repeated, therefore, it is not iterative print ('=>') l = [, 3] count = 0 while count <len (l): # iterative print (l [count]) count + = 1Ii. Why is there an iterator, what is an iteratable object, and what is an iterator object?
#1. Why is there an iterator? For the sequence type: String, list, And tuples, we can use the index method to iteratively retrieve the contained elements.
However, no indexes are available for dictionary, set, and file types. If you want to retrieve the elements contained in the dictionary, you must find an index-independent iteration method, this is the iterator #2. What is an iteratable object? An iteratable object refers to an object with a built-in _ iter _ method, that is, obj. _ iter __, 'hello' as follows '. _ iter _ (1, 2, 3 ). _ iter _ [1, 2, 3]. _ iter __{ 'A': 12.16.w.iter={'a', 'B '}.w.iter=open('a.txt '). _ iter __# 3. What is an iterator object? Iterator '). _ next _ () #4. Note: The iterator object must be an iteratable object, but the iteratable object may not be an iterator object.
Iii. Use of iterator objects
Dic = {'A': 1, 'B': 2, 'C': 3} iter_dic = dic. _ iter _ () # obtain the iterator object. The iterator object includes _ iter _ and _ next __, but: iterator. _ iter _ () still obtains iter_dic of the iterator itself. _ iter _ () is iter_dic # Trueprint (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 an exception StopIteration, or an end sign # With the iterator, we can take iter_dic = dic without relying on the index iteration value. _ iter _ () while 1: try: k = nex T (iter_dic) print (dic [k]) Comment t StopIteration: break # This write is too ugly. We need to capture exceptions and control next. can we solve this problem for python? Yes. Please refer to the for LoopIV. for Loop
# Based on the for loop, we can no longer rely on the index to set the value to dic = {'A': 1, 'B': 2, 'C': 3} for k in dic: print (dic [k]) # Working Principle of the for Loop #1: The dic of the object after the in statement is executed. _ iter _ () method to obtain an iterator object iter_dic #2: Execute next (iter_dic), assign the value to k, and then execute the loop body Code #3: repeat process 2 until the exception StopIteration is caught and the loop ends.V. Advantages and disadvantages of the iterator
Advantages: 1. provides a unified, index-independent Iteration Method
2. Lazy computing, with only one piece of data at a time, saving memory
Disadvantages: 1. The length cannot be obtained (the number of values can be known only after iteration)
2. One-time, sequential movement, cannot be withdrawn
Generator 1. What is a generator?
The function contains the yield keyword. Internal code is not executed when the function is called. The returned value of the function is a generator object.
Def chicken (): print ('=> first') yield 1 print ('=> sencond ') yield 2 print ('====> third') yield 3obj = chicken () print (obj) # <generator object func at 0x> generator object2. Generator is the iterator
Generator is essentially an iterator. That is to say, the generator is actually an iterator.
Obj. _ iter _ obj. _ next __# 2. Therefore, the generator is an iterator. Therefore, you can set this value to res = next (obj) print (res)
Mimic the range () function
def my_range(start,stop,step=1): while start < stop: yield start start+=step
For item in my_range (1, 10, 2): print (item)
# Result:, 9
Summary: yield
#1. We provide a way to customize the iterator. # The yield keyword can be used in the function. The result obtained by calling the function is a generator, generator is the iterator #2. yield can be used to return values like return. The difference is that return can only return values once, while yield can return values multiple times # Because yield can save the state of function execution