Various derivation analysis of Python

Source: Internet
Author: User
Tags generator

The derivation of comprehensions (also known as analytic) is a unique feature of Python. Derivation is the structure of another new data series that can be built from one data series. There are three kinds of derivation, which are supported in Python2 and 3:

    • List-derived
    • Dictionary (dict) derivation formula
    • Set (set) derivation

1. Use [] to generate list

Basic format

variable = [out_exp_res for out_exp in input_list if out_exp = = 2]  out_exp_res: List generates an element expression, which can be a function with a return value. For  Out_exp in input_list: Iteration input_list out_exp incoming out_exp_res expression.  if Out_exp = = 2: Filter which values can be based on conditions.

Example one:

multiples = [I for I in range] if I percent 3 is 0]print (multiples) # Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]

Example two:

def squared (x):    return x*xmultiples = [Squared (i) for I in range () if I% 3 is 0]print multiples#  Output: [0, 9, 36, 81, 144, 225, 324, 441, 576, 729]

2. Use () to generate generator

The generator can be obtained by changing the two-table derivation [] to ().

Multiples = (I for I in range () If I percent 3 is 0) print (type (multiples)) #  Output: <type ' generator ' >

Second, the dictionary derivation formula

Dictionary derivation and list derivation are used in a similar way, except that the brackets should be changed to curly braces. The direct example illustrates:

Example one: Capitalization key merge

Mcase = {' A ': ten, ' B ': ", ' a ': 7, ' Z ': 3}mcase_frequency = {    k.lower (): Mcase.get (K.lower (), 0) + mcase.get (K.upper () , 0) for    K in Mcase.keys ()    if K.lower () in [' A ', ' B ']}print mcase_frequency#  Output: {' A ': +, ' B ': 34}

Example two: Quick change of key and value

Mcase = {' A ': ten, ' B ': 34}mcase_frequency = {v:k for k, V in Mcase.items ()}print mcase_frequency#  Output: {: ' A ', 3 4: ' B '}

Three, set deduction formula

They are similar to the list derivation. The only difference is that it uses curly braces {}.

Example one:

Squared = {x**2 for x in [1, 1, 2]}print (squared) # output:set ([1, 4])

Various derivation analysis of Python

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.