List generation and generator for Python

Source: Internet
Author: User

1. List generation is one of the most popular syntax for Python, with a simple syntax to filter a set of elements and to convert the resulting elements into a syntax format:

 for inch if condition]

Equivalent

Resul = [] for in collection:    if(condition):        Result.appen (exp)

Example: Ask 1 to 10 of even squares, the code is as follows:

Li = [x*x for in  if x%2==0]print (LI)

Results: [4, 16, 36, 64, 100]

Explain:

    • Take out the number xrange (1,11) from 1 to 10 in turn
    • Judge X*x is an even number, keep it, there is a new list
    • Put all elements that conform to X*X are returned in the new list

2. Generator

With list generation, we can create a list directly, but with memory limitations, the list capacity is bound to be 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, 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).

To create a generator, there are a number of ways, the first method is simple, as long as a list-generated [] is changed to (), a generator is created:

 for inch if  x%2 = = 0)print  (LT)print  (type (LT)) for in LT:     Print (i)

It can be printed by calling the next () function. But the correct way is to use a for loop. Results:

The second approach is to use the yield keyword to define, for example:

def Funb (n):     = 0    = 0    while (i<N):        = sum +i        i+=1         Yield(sum)print (Type (FUNB)) for in Funb (Ten) :    print (x)

Results:

Explanation: 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 call to next (), encounters a yield statement return, and executes again from the yield statement that was last returned.

Summary: Generator is a very powerful tool, in Python, you can simply change the list generation to generator, or you can implement complex logic generator 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.

3. The difference between a build and a generator

Generated: all data is generated at once, then stored in memory, suitable for a small amount of data

Generator: Returns an iterative object and a generator object that must be looped to fetch all the results

Iterative objects: can be called through the loop, is the object can be iterated, such as list,tuple,dict, Generation, generator
Iterator: An object called by the next () function that continually returns the next value is known as an iterator

List generation and generator for Python

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.