Python-derived (comprehensions)

Source: Internet
Author: User
Tags generator

Tag: print IMA image resolves return code for with ITER

First, Introduction

Listing derivation (list comprehensions)

This is a way to put a for loop, if expression, and an assignment statement into a single statement. In other words, you can use an expression to map or filter a list.

A list deduction consists of the following sections:

1, an input sequence

2. A variable representing the input sequence member

3. An optional assertion expression

4. An output expression that transforms a member of an input sequence that satisfies an assertion expression into an output list member

Ii. examples

If you need to generate a new list of all integer squares greater than 0 from the list, you might write:

    

Num_list = [11,2,-33,10,7,3,5,43= [] for in num_list:    if num > 0:        squared_list.append (num**2)print  squared_list# [121, 4, +, 9, 1849]

It's simple, isn't it? But this will have 4 lines of code, two layers of nesting plus a completely unnecessary append operation. If you use the filter, lambda, and map functions, you can greatly simplify your code:

Num_list = [11,2,-33,10,7,3,5,43= map (lambda x:x * * 2,filter (lambda x:x>0,num_list) )print  squared_list#  [121, 4, +, 9, 1849]

Use list derivation below

Num_list = [11,2,-33,10,7,3,5,43for inif x >0]print  squared_list#  [121, 4, +, 9, 1849]

Third, the description

    

1, iterator (iterator) iterates through the input sequence each member of Num x

2, assert the judge whether each member is greater than 0

3. If the member is greater than 0, the output expression is given, and the square becomes the member of the output list.

The list derivation is encapsulated in a list, so it is obvious that it can generate a new list immediately. There is only one type function call and no implicit invocation of the lambda function, and the list derivation uses a regular iterator, an expression, and an if expression to control the optional arguments.

On the other hand, the list derivation may have some negative effects, that is, the entire list must be loaded in memory at once, which is not a problem for the example above, and even after several times the expansion is not a problem. But the limit is always reached and memory is always exhausted.

For the above problem, the generator (Generator) can be solved very well. The generator expression does not load the entire list into memory at one time, but instead generates a generator object (Generator objector), so only one list element is loaded at a time.

The builder expression has almost the same grammatical structure as the list derivation, except that the generator expression is surrounded by parentheses, not square brackets, and, unless there are special reasons, you should often use generator expressions in your code. But unless you are facing a very large list, you will not see the obvious difference.

Num_list = [11,2,-33,10,7,3,5,43]squared_listfor inif x >0)  Print  squared_list#  [121, 4, +, 9, 1849]

This is slightly more efficient than the list derivation, so let's change the code once again:

Num_list = [11,2,-33,10,7,3,5,43]def  Square_generator (p):    returnfor  inif x > p)print  square_generator (0)#  [121, 4 , 9, 1849]print square_generator#  [121, 1849]

Python-derived (comprehensions)

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.