Python Learning---Builder

Source: Internet
Author: User

With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 10 million elements takes up a lot of storage space, and if we just need to access the first few elements, the vast majority of the space behind it is wasted.

So, if the list element can be calculated according to an algorithm, can we continue to calculate the subsequent elements in the process of the loop? This eliminates the need to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the Generator (Generator).

There are a number of ways to create a generator.

The first method is simple, as long as a list of the [] generated formula () is changed to create a generator:

1. Simple List operation:

1>>> List1 = [x*x forXinchRangeTen)]2>>>List13[0,1,4,9, -, -, $, the, -,Bayi]4>>> List2 = [2*x forXinchRange A)]5>>>List26[0,2,4,6,8,Ten, A, -, -, -, -, A]7>>>

2. Generator Action:

1>>> List3 = (x*x forXinchRangeTen))2>>>List33<generatorObject<genexpr> at0x7f302fdb6cd0>4>>>List15[0,1,4,9, -, -, $, the, -,Bayi]6>>>List27[0,2,4,6,8,Ten, A, -, -, -, -, A]8>>>

As the code shows, List1 and List2 are a list, and List3 is a generator, and at any moment we can print out the list elements of List1 and List2 casually, but how can we print out the LIST3 elements? , where generator provides a function for printing its own elements , next ().

1>>>List32<generatorObject<genexpr> at0x7f302fdb6cd0>3>>>List3.next ()4 05>>>List3.next ()6 17>>>List3.next ()8 49>>>List3.next ()Ten 9 One>>>List3.next () A  - ->>>List3.next () -  - the>>>List3.next () -  $ ->>>List3.next () -  the +>>>List3.next () -  - +>>>List3.next () A Bayi at>>>List3.next () - Traceback (most recent): -File"<stdin>", line1,inch<module> - stopiteration ->>>

Generator saves the algorithm, each time it next() is called, computes the value of the next element until the last element is computed, and when there are no more elements, the stopiteration error is thrown.

Using the next method although you can calculate the value of the generator, but it is too impersonal, here, there is no better way to output the value of the generator? The method must be there, that is, with the for iteration output:

1>>> List4 = (x*x forXinchRangeTen))2>>>List33<generatorObject<genexpr> at0x7f302fdb6cd0>4>>> forNinchList4:5 ... print N6 ... 7 08 19 4Ten 9 One  - A  - -  $ -  the the  - - Bayi ->>>

So, after we create a generator, we basically never call next() the method, but iterate through the for loop.

Generator is very powerful. If the calculated algorithm is more complex, and the loop with similar list generation for cannot be implemented, it can also be implemented with functions.

For example, the famous Fibonacci sequence (Fibonacci), except for the first and second numbers, can be summed up by the top two numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The Fibonacci sequence is not written in a list, but it is easy to print it out with a function:

1>>>2>>>def fibla (max):3... n,a,b =0,0,14... whileN <Max:5 .. print B6... B = b,a+b7... n = n +18 ... 9>>> Fibla (8)Ten 1 One 1 A 2 - 3 - 5 the 8 -  - -  + ->>>

After observation, it is found that the Fibla function is actually the calculation rule that defines the Fibonacci sequence, which can be deduced from the first element, and the subsequent arbitrary elements, which are very similar to generator.

In other words, the above functions and generator are only a step away. To turn fib a function into a generator, just print b change yield b it to:

1>>>def fibla (max):2... n,a,b =0,0,13... whileN <Max:4 .. print B5... B = b,a+b6... n = n +17 ... 8>>> Fibla (8)9 1Ten 1 One 2 A 3 - 5 - 8 the  - -  + - #上下对比 ->>>def fibla (max): +... n,a,b =0,0,1 -... whileN <Max: +...yieldb A... B = b,a+b at... n = n +1 - ...  ->>> Fibla (8) -<generatorObjectFibla at0x7f302fd5ca00> ->>>

This is another way to define generator. If a function definition contains a yield keyword, then the function is no longer a normal function, but a generator:

The most difficult thing to understand here is that the generator is not the same as the execution flow of the function. The function is executed sequentially, the return statement is encountered, or the last line function statement is returned. The function that becomes generator, executes at each invocation next() , encounters a yield statement return, and executes again from the last statement returned yield .

1>>>def fibla (max):2... n,a,b =0,0,13... whileN <Max:4...yieldb5... B = b,a+b6... n = n +17 ... 8>>> Fibla (8)9<generatorObjectFibla at0x7f302fd5ca00>Ten>>> forXinchFibla (8): One ... print x A ...  - 1 - 1 the 2 - 3 - 5 - 8 +  - -  + +>>>

Python Learning---Builder

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.