Python list derivation, Dictionary derivation, set derivation usage instance analysis, python instance analysis

Source: Internet
Author: User
Tags python list

Python list derivation, Dictionary derivation, set derivation usage instance analysis, python instance analysis

This article describes the Python list derivation, Dictionary derivation, and set derivation usage. We will share this with you for your reference. The details are as follows:

The Derivation comprehensions (also known as the analytical expression) is a unique feature of Python. The derivation is a structure that can be used to construct a new data sequence from one data sequence. There are three kinds of derivation supported in Python2 and 3:

List Derivation
Dictionary (dict) Derivation
Set Derivation

1. List Derivation

1. Use [] to generate a list

Basic Format

Variable = [out_exp_res for out_exp in input_list if out_exp = 2]

Out_exp_res: List Generation Element expression, which can be a function with a returned value.
For out_exp in input_list: iteration input_list transfers out_exp to the out_exp_res expression.
If out_exp = 2: Which values can be filtered Based on the condition.

Example 1:

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

Example 2:

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

2. Generate a generator using ()

Change the two table's deduced [] to () to get the generator.

multiples = (i for i in range(30) if i % 3 is 0)print(type(multiples))# Output: <type 'generator'>

Ii. dictionary Derivation

Dictionary derivation is similar to list derivation. You only need to change the brackets to braces. Examples:

Example 1: case-sensitive key Merging

mcase = {'a': 10, 'b': 34, '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': 17, 'b': 34}

Example 2: Quickly change the key and value

mcase = {'a': 10, 'b': 34}mcase_frequency = {v: k for k, v in mcase.items()}print mcase_frequency# Output: {10: 'a', 34: 'b'}

Iii. Set Derivation

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

Example 1:

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

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.