current position:Home>Detailed explanation of Python derivation

Detailed explanation of Python derivation

2022-01-31 09:43:56 tigeriaf

This is my participation 11 The fourth of the yuegengwen challenge 10 God , Check out the activity details :2021 One last more challenge .

The derived type , Also known as analytic , yes Python A unique characteristic of , The derivation is that the structure of another new data sequence can be constructed from one data sequence , There are three derivations :

  • List derivation
  • Dictionary derivation
  • Set derivation

Next, we will introduce .

List derivation

grammar :

  • Variable name = [< expression > for < Variable > in < iterator >,...]
  • Variable name = [< expression > for < Variable > in < iterator > if < Conditions >]

Its structure is in a bracket [] Contains an expression , Then there is one or more for Loop statement , Traverse the elements in the sequence and assign values to variables , The operation is based on the value of the variable , The expression can be arbitrary . There is if When judging a statement , Make a judgment first , The elements that meet the conditions are then calculated for the expression , The result is a new list , In order to if and for Statement is a new list generated after the expression of the context is run .


  • Create a 100 List of all integers within :
list1 = [i for i in range(100)]
 Copy code 
  • Create a 100 List of all even numbers within :
list1 = [i for i in range(100) if i % 2 == 0]
 Copy code 

There are two lists [1,2,3] and [7,8,9], Create a list containing the product of the elements of the two lists :

print([a*b for a in [1,2,3] for b in [7,8,9]])
# [7, 8, 9, 14, 16, 18, 21, 24, 27]
 Copy code 

Set derivation

Set derivation is similar to list derivation , The grammar is the same , The only difference is that the set derivation uses braces {}, The result is a collection . because Python The set in set Is an unordered sequence of non-repeating elements , Therefore, the set derivation also has its own de duplication effect .

for example , Calculate the square of the elements in the list :

set1 = {i*i for i in [1,2,2,3]}
# {1, 4, 9}
 Copy code 

Dictionary derivation

The use of dictionary derivation and list derivation is similar , The grammar is almost the same , It's just that the result is a dictionary .

for example , Replace all the keys in the dictionary with uppercase :

dict1 = {'a': 10, 'b': 20, 'c': 30, 'd': 40}
dict2 = {k.upper(): v for k, v in dict1.items()}
# {'A': 10, 'B': 20, 'C': 30, 'D': 40}
 Copy code 

All in all , When processing a sequence , Derivation is an efficient and concise way , But when you need to create all kinds of temporary intermediate data in the loop , The derivation is no longer appropriate , Still use for Loop processing .

Originality is not easy. , If the partners feel helpful , Please give me a compliment before you leave ~

Last , Thank you for your tolerance in work and life 、 Understanding and supporting !

copyright notice
author[tigeriaf],Please bring the original link to reprint, thank you.

Random recommended