Python face question generator/iterator

Source: Internet
Author: User

1. Why do I have a generator?

With list generation, we can create a list directly. However, with memory limitations, the list capacity is certainly limited. Also, creating a list of 1 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, then whether we can ...

The subsequent elements are continuously calculated during the loop, so that you do not have to create a complete list, which saves a lot of space. In Python, this side loop computes the mechanism, called the generator: Generator.

The first method is very simple, as long as a list of the generation of the [] change () , the creation of a generator, is actually a tuple.

L = [x * x forXinchRange (10)]Print(L)Print(Type (L)) L2= (x * x forXinchRange (10))Print(L2)Print(Type (L2))#[0, 1, 4, 9, +,-#<class ' list ' >#<generator Object <genexpr> at 0x00fe4fc0>#<class ' generator ' >

Lthe difference between creating and L2 is only the outermost [] and () the L list, and L2 is a generator.

We can print out every element of the list directly, but how do we print out every element of generator?

If you want to print out one, you can next() get the next return value for generator by using a function:

def fib (max):     = 0, 0, 1 while     n < Max:        print(b)        = B, A + b        
    = n + 1    return'done'

In other words, the above functions and generator are only a step away. To turn a fib function into a generator, you just need to print(b) change yield b it, and that's 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:

Generator is a very powerful tool, in Python, you can simply change the list generation to generator, or you can implement the generator of complex logic through functions.

To understand how generator works, it is continuously calculating the next element during the for loop and ending the For loop in the appropriate condition. For the generator of a function, a return statement or execution to the last line of the function body is the end of the generator instruction, and the For loop ends with it.

Notice that the normal function and the generator function are distinguished, and the ordinary functions call to return the result directly:

>>> r = ABS (6)
>>> R
6
The "call" of the generator function actually returns a generator object:

>>> g = fib (6)
>>> g
<generator Object fib at 0x1022ef948>

Python face question generator/iterator

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.