Explanation of the Principle and Use of Python Generator

Source: Internet
Author: User
Keywords python generator python generator principle
0.range() function, its function is to create a list of integers, generally used in for loop
Syntax format: range(start, stop, step), the parameter reference is as follows:

start: The count starts from start. The default is to start from 0. For example, range(4) is equivalent to range(0, 4); the result: (0,1,2,3)
stop: count to the end of stop, but stop is not included. For example: range(0, 5) is [0, 1, 2, 3, 4] without 5
step: step length, default is 1. For example: range(0, 5) is equivalent to range(0, 5, 1)
#Use the range function to build the list
ls =[x*2 for x in range(10)]
print(ls)#[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
 
ls1 = [x for x in range(0,10,2)] #Step size is 2.
print(ls1) #[0, 2, 4, 6, 8]
 
ls2 = [x for x in range(3,10,2)] #Start from 3, the step size is 2.
print(ls2) # [3, 5, 7, 9]
 
ls3 =[x for x in range(0, -10, -1)] #Use of negative numbers
print(ls3) #[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  
print(range(0)) #range(0, 0)
print(range(1,0)) #range(1, 0)

1. Generator creation and element iteration
1.1 Create a generator method 1: Just change [] of a list generator to ()
Generator is actually a special kind of iterator. In the previous blog, each time we iterate through the data (through the next() method), it is generated according to a specific rule. But when we implement an iterator, we need to record our own state about the current iteration to generate the next data according to the current state. In order to record the current state and iterate with the next() function, Python has a generator. So generator is actually a special kind of iterator.

#1. Create a generator
ls = [x*2 for x in range(10)]
generator1 =(x*2 for x in range(10)) #This is a generator generator
print(ls)
print(generator1) #Note that the print generator will not print his value like a list, but the address.
'''
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
<generator object <genexpr> at 0x00000239FE00A620>
'''

 
 
1.1 traversal generator content
Traverse the contents of the generator object:
1. Method 1. Use a for loop to traverse
for i in generator1:
    print(i)
 
#Method 2: Use the next() function on the command line: call next(G) to calculate the value of the next element of G until the last element is calculated
When there are no more elements, a StopIteration exception is thrown.
>>> generator1 =(x*2 for x in range(5))
>>> next(generator1)
0
>>> next(generator1)
2
>>> next(generator1)
4
>>> next(generator1)
6
>>> next(generator1)
8
>>> next(generator1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2. Method 2. The python script uses the next() method. In actual development, it is traversed through a for loop. This next() method is too cumbersome.
g1 = (x*2 for x in range(5))
while True:
    try:
        x = next(g1)
        print(x)
    except StopIteration as e:
        print("values=%s"%e.value)
        break #Note that break is added here, otherwise it will endlessly loop.
'''The results are as follows:
0
2
4
6
8
values=None
'''
3. Method 3: Use the object's own __next__() method, the effect is equivalent to the next(g1) function
>>> g1 = (x*2 for x in range(5))
>>> g1.__next__()
0
>>> g1.__next__()
2
>>> g1.__next__()
4
>>> g1.__next__()
6
>>> g1.__next__()
8
>>> g1.__next__()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>>

1.2 Create a generator method 2: Use the yield function to create a generator.
The generator is very powerful. If the calculation algorithm is more complicated, when the for loop similar to the list generation type cannot be realized, it can also be implemented with a function. In short: as long as there is a yield keyword in the def, it is called a generator

#Famous Fibonacci sequence (Fibonacci): In addition to the first and second numbers, any number can be obtained by adding the first two numbers
#1. Example: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... use the function to print any first n items of the sequence.
 
def fib(times): #times means to print the first time bits of the Fibonacci sequence.
    n = 0
    a,b = 0,1
    while n<times:
        print(b)
        a,b = b,a+b
        n+=1
    return'done'
 
fib(10) #Top 10: 1 1 2 3 5 8 13 21 34 55
 
#2. Replace print(b) with yield b, then the function will become a generator.
#yield b function is: every time when there is a yield, the value of b after the yield will be returned to the function and the function will pause until the next call or iteration is terminated;
def fib(times): #times means to print the first time bits of the Fibonacci sequence.
    n = 0
    a,b = 0,1
    while n<times:
        yield b
        a,b = b,a+b
        n+=1
    return'done'
 
print(fib(10)) #<generator object fib at 0x000001659333A3B8>
 
3. Iterate over the elements of the generator
Method 1: Use a for loop
for x in fib(6):
    print(x)
''''The results are as follows. If you find how the generator is a function, you cannot get the return value of the function using for traversal.
1
1
2
3
5
8
'''
Method 2: Use next() function to traverse the iteration, you can get the return value of the generator function. Similarly, you can also use the built-in __next__() function with the same effect
f = fib(6)
while True:
    try: #Because non-stop calling next will report an exception, so catch and handle the exception.
        x = next(f) #Note that next(fib(6)) cannot be written directly here, otherwise it will be called repeatedly every time
        print(x)
    except StopIteration as e:
        print("Generator return value: %s"%e.value)
        break
'''The results are as follows:
1
1
2
3
5
8
Generator return value: done
'''

Summary of generator usage:
1. The advantage of the generator is that it can be calculated while looping, without generating a large collection at once, occupying memory space. The use of generators saves memory space.
2. The generator saves the algorithm, and the list saves the calculated content, so if the same content is used, the generator takes up less memory, and the list takes up more memory. Each time next(G) is called, the value of the next element of G is calculated until the last element is calculated, and when there are no more elements, a StopIteration exception is thrown.
3. Use a for loop to traverse the contents of the generator, because the generator is also an iterable object. Iterate it through a for loop, no need to care about the StopIteration exception. However, when the generator is called with a for loop, the return value of the generator's return statement cannot be obtained. If you want to get the return value, you must use the next () method, and capture StopIteration error, the return value is included in the value of StopIteration.
4. In Python, functions that use yield can be called generators. A generator is a function that returns an iterator and can only be used for iterative operations. It is simpler to understand that a generator is an iterator.
5. A function with yield is a generator. Unlike a normal function, generating a generator looks like a function call, but does not execute any function code until next() is called on it (it is automatically called in the for loop) next()) starts execution. Although the execution flow is still executed according to the flow of the function, every time a yield statement is executed, it will be interrupted, save all the current running information, and return an iteration value. The next execution of the next () method will continue from the next statement of yield . It looks as if a function was interrupted by yield several times during normal execution, and each interrupt will return the current iteration value through yield. Not only does the generator "remember" its data state; the generator also "remembers" its position in the flow control construct.
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.