Python iterators and generators

Source: Internet
Author: User
Tags function definition generator generator

Review the For loop in Python

If you use an index to take a value, you can take the value anywhere, if you want to know where the value is.

If we use a for loop to get the value, we take each value, do not need to care about the location of each value, because only the order of the value, and can not skip any one directly to fetch the value of another location.

What do you mean by iteration

"Can iterate", it should be able to be used for loop

strings, lists, tuples, dictionaries, and collections can all be used for loops, indicating that they are both iterative

The data in a dataset can be taken out "one next to another", which is called an iteration.

an iterative protocol

If we write a data type ourselves and hope that something in this data type can be taken out with a for by one, then we have to satisfy the for requirement. This requirement is called " Agreement ".

a requirement that can be iterated over is called an iterative protocol. the definition of an iterative protocol is very simple, which is to implement the __iter__ method internally.

Iterator iterator

Iterators follow an iterator protocol: you must have the __iter__ method and the __next__ method.

The For loop is an iterator-based protocol that provides a uniform way to traverse all objects.

That is, before the traversal, the object's __iter__ method is called to convert it to an iterator,

The iterator protocol is then used to iterate through the loop, so that all objects can be traversed by a for loop

Generator

There are two kinds of iterators: one is to return the call method directly, and one is to get the iterative object by executing the ITER method, and the advantage of the iterator is that it can save memory.

If in some cases, we also need to save memory, we can only write ourselves. What we write ourselves is called the generator, which implements the function of the iterator.

The generators available in Python:

1. Generator function: General function definition, however, returns the result using the yield statement instead of the return statement. The yield statement returns one result at a time, in the middle of each result, suspends the state of the function so that the next time it leaves, it resumes execution.

2. Generator expression: Similar to list derivation, however, the generator returns an object that produces results on demand, rather than building a list of results at a time

Generator generator:

Essence: iterators (so we have the __iter__ method and the __next__ method, we do not need to implement)

Features: Lazy operation, developer customization

Generator functions

A function that contains the yield keyword is a generator function. Yield can return a value from a function, but yield is different from return,

The execution of the return means the end of the program, and the Call builder function does not get the specific value returned, but instead gets an object that can be iterated.

Each time you get the value of this iterative object, you can push the execution of the function to get a new return value. Until the function execution is finished.

Import Timedefgenrator_fun1 (): a= 1Print('The A variable is now defined')    yielda B= 2Print('and now we've defined the B variable.')    yieldbg1=genrator_fun1 ()Print('G1:', G1)#print G1 can be found G1 is a generatorPrint('-'*20)   Print(Next (G1)) Time.sleep (1)#sleep a second to see the execution processPrint(Next (G1)) first knowledge of generator functions

The benefit of the generator is that it doesn't generate too much data in memory at a sudden

#First Knowledge Generator twodefProduce ():"""Production of clothing"""     forIinchRange (2000000):        yield "produced the first piece of%s clothes"%Iproduct_g=Produce ()Print(Product_g.__next__())#want a piece of clothingPrint(Product_g.__next__())#I need another dress .Print(Product_g.__next__())#I need another dress .num =0 forIinchProduct_g:#want a batch of clothes, like 5 pieces    Print(i) Num+=1ifnum = = 5:         Break#here we have 8 pieces of clothes for the factory, and I have made my production function (that is, the produce generator function) to produce 2 million pieces of clothing. #There's a lot of clothes left, we can keep them, we can keep them when we want to take them.First Knowledge Generator two
Import Timedeftail (filename): F=open (filename) f.seek (0,2)#count from the end of the file     whileTrue:line= F.readline ()#read new lines of text in a file        if  notLine:time.sleep (0.1)            Continue        yieldLinetail_g= Tail ('tmp') forLineinchTail_g:Print(line) Generator Listener example of file input
defgenerator ():Print(123) Content=yield1Print('=======', content)Print(456) yield2g=generator () ret= G.__next__()Print('***', ret) ret= G.send ('Hello')#the effect of send is the same as nextPrint('***', ret)#the effect of send to get the next value is basically the same as next#just to get the next value, pass a data to the last yield position#Considerations for using Send    #The first time you use the generator, you get the next value with next.    #the last yield cannot accept external values
defgen1 (): forCinch 'AB':        yieldC forIinchRange (3):        yieldIPrint(List (Gen1 ()))defGen2 ():yield  from 'AB'    yield  fromRange (3)Print(List (Gen2 ()))yield  from
View Code
 def   Averager (): Total  = 0.0 count  = = None  while   true:term  = yield   average total  += term count  + = 1 average  = Total/countg_avg  = Averager () Next (G_AVG)  print  (g_avg.send (10))  print  (g_avg.send (30 print  (g_avg.send (5 1) 
View Code
defInit (func):#when invoking the decorator generator function, first activate the generator with next    defInner (*args,**Kwargs): G= Func (*args,**Kwargs) Next (g)returngreturnInner@initdefAverager (): Total= 0.0Count=0 Average=None whileTrue:term=yieldaverage Total+=Term Count+ = 1Average= total/Countg_avg=Averager ()#Next (G_AVG) executes the next method in the adornerPrint(G_avg.send (10))Print(G_avg.send (30))Print(G_avg.send (5) ) calculates the moving average (2) _ Pre-excitation co-process decorator
View Code

list derivation and generator expressions

1. The [] Conversion of the list parsing [] to () is the generator expression

2. List parsing and builder expressions are a convenient way of programming, except that generator expressions are more memory-efficient

3.Python not only uses the iterator protocol, but makes the for loop more general. Most built-in functions also access objects using an iterator protocol. For example, the SUM function is a python built-in function that accesses an object using an iterator protocol, and the generator implements an iterator protocol, so we can calculate the sum of a series of values directly:

SUM (x * * 2 for X in range (4))

Instead of superfluous, construct a list first:

Other derivation http://www.cnblogs.com/Eva-J/articles/7276796.html

Python iterators and generators

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.