Python Generator generator

Source: Internet
Author: User

First, generator definition

By generating an expression from a list, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. 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.

1  for  in range (ten)]2 >>> l3 [0, 1, 4, 9, +,- C8>4 for in range5 >>> g6 < Generator Object <genexpr> at 0x1013e0780>

Second, Expression Builder

The difference between the creation and the L is g only the outermost [] and (), L is a list, and g it is a generator. Each element of L can be printed directly, and the next () function is required to print out each element of G.

1>>> g = (x * x forXinchRange (10))2>>>g3<generator Object <genexpr> at 0x1013e0780>4>>>Next (g)5 06>>>Next (g)718>>>Next (g)94Ten>>>Next (g) One9 A>>>Next (g) -16 ->>>Next (g) the25 ->>>Next (g) -36 ->>>Next (g) +49 ->>>Next (g) +64 A>>>Next (g) at81 ->>>Next (g) - Traceback (most recent): -File"<stdin>", Line 1,inch<module> -Stopiteration

Generator saves the algorithm, each time it is called next(g) , computes g the value of the next element until it is calculated to the last element, no more elements are thrown when the StopIteration error occurs.

1>>> g = (x * x forXinchRange (10))2>>> forNinchg:3...Print(n)4 ... 5 0617489916Ten25 One36 A49 -64 -81

First, generator is an iterative object, so you can iterate through it using the for in loop. the nature of the traversal is that the for in loop internally calls the next () function to get each element, and captures the stopiteration exception, ending the traversal.

Third, Function generator

The Fibonacci sequence is not written in list generation, but it is easy to print it out using a function.

1>>>deffib (max):2... n, a, b = 0, 0, 13... whileN <Max:4...yieldb5... a, B = B, A +b6... n + = 17...RaiseStopiteration (' Done')8 ... 9>>> FIB (6)Ten<generator Object fib at 0x1013e0780> One>>> forIinchFIB (6): A...Print(i) - ...  -1 the1 -2 -3 -5 +8 ->>> g = fib (6) +>>> whileTrue: A...Try: at ... next (g) -...exceptstopiteration as E: -...Print(E.value) -... Break - ...  -1 in1 -2 to3 +5 -8 theDone

Python Generator generator

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.