The derivation is a very powerful and popular feature in Python, with the advantages of simple language and fast speed. The derivation formula includes:
1. List-derived
2. Dictionary derivation formula
3. Set Deduction formula
Nested-List derivation
Note: Dictionaries and set derivations are only recently added to Python (Python 2.7 and Python version 3.1 or later). The following is a brief introduction:
"List-derived"
List derivation can be very concise to construct a new list: The resulting element can be transformed with a simple expression
Its basic format is as follows:
Copy the Code code as follows:
[Expr for value in collection Ifcondition]
Filter conditions are optional, depending on the actual application, leaving only the expression, equivalent to the following for loop:
Copy CodeThe code is as follows:
result = []
For value in collection:
If condition:
Result.append (expression)
Example 1: Filter out a list of strings that are less than 3 long and convert the rest to uppercase
Copy CodeThe code is as follows:
>>> names = [' Bob ', ' Tom ', ' Alice ', ' Jerry ', ' Wendy ', ' Smith ']
>>> [Name.upper () for name in names if Len (name) >3]
[' ALICE ', ' JERRY ', ' WENDY ', ' SMITH ']
Example 2: (x, y) where x is an even number between 0-5 and Y is an odd-numbered list of Ganso between 0-5
Copy CodeThe code is as follows:
>>> [(x, Y) for x in range (5) if x%2==0 for Y in range (5) if y%2==1]
[(0, 1), (0, 3), (2, 1), (2, 3), (4, 1), (4, 3)]
Example 3: Finding a list of 3,6,9 in M
Copy CodeThe code is as follows:
>>> M = [[+],
... [4,5,6],
... [7,8,9]]
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [row[2] for row in M]
[3, 6, 9]
#或者用下面的方式
>>> [m[row][2] for row in (0,1,2)]
[3, 6, 9]
Example 4: Find a list of 1,5,9 in M slash
Copy CodeThe code is as follows:
>>> M
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> [M[i][i] for I in range (len (M))]
[1, 5, 9]
Example 5: Finding the product of matrices and elements in M,n
Copy CodeThe code is as follows:
>>> M = [[+],
... [4,5,6],
... [7,8,9]]
>>> N = [[2,2,2],
... [3,3,3],
... [4,4,4]]
>>> [M[row][col]*n[row][col] for row in range (3) for Col in Range (3)]
[2, 4, 6, 12, 15, 18, 28, 32, 36]
>>> [[M[row][col]*n[row][col] for Col in Range (3)] for row in range (3)]
[[2, 4, 6], [12, 15, 18], [28, 32, 36]]
>>> [[M[row][col]*n[row][col] for row in range (3)] for Col in Range (3)]
[[2, 12, 28], [4, 15, 32], [6, 18, 36]]
Example 5: Speaking of the Age key in a dictionary, assigning new values by criteria
Copy CodeThe code is as follows:
>>> Bob
{' pay ': +, ' job ': ' Dev ', ' age ': $, ' name ': ' Bob Smith '}
>>> Sue
{' Pay ': 4000, ' job ': ' HDW ', ' Age ': ' ' name ': ' Sue Jones '}
>>> people = [Bob, Sue]
>>> [rec[' age ']+100 if rec[' age '] >= (Else rec[' age ') for Rec in people] # Note for location
[42, 145]
"dictionary-derived"
Dictionary and set derivation is the continuation of the idea, the syntax is similar, but the result is a set and a dictionary. Its basic format is as follows:
Copy the Code code as follows:
{key_expr:value_expr for value in collection if condition}
Example 1: Using a dictionary derivation to build a dictionary with a string and its length
Copy CodeThe code is as follows:
>>> strings = [' Import ', ' is ', ' with ', ' if ', ' file ', ' exception ']
>>> D = {Key:val for Val,key in Enumerate (strings)}
>>> D
{' Exception ': 5, ' is ': 1, ' file ': 4, ' import ': 0, ' with ': 2, ' If ': 3}
"Set Derivation"
The set deduction is very similar to the list derivation, except that the {} is the only difference. Its basic format is as follows:
Copy CodeThe code is as follows:
{Expr for value in collection if condition}
Example 1: Building a set of string lengths with set derivation
Copy CodeThe code is as follows:
>>> strings = [' A ', ' is ', ' with ', ' if ', ' file ', ' exception ']
>>> {len (s) for s in Strings} #有长度相同的会只留一个, which is also useful in practice
Set ([1, 2, 4, 9])
"nested list derivation"
Nested lists are nested lists in a list, such as:
Copy CodeThe code is as follows:
>>> L = [[+],
[4,5,6],
[7,8,9]]
Example 1: A nested list of men's lists and women's lists, with names containing more than two letters E in their names, to form a list
Copy CodeThe code is as follows:
names = [[' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' Steven ', ' Joe '],
[' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' Jennifer ', ' Sherry ', ' Eva ']
Implemented with A For loop:
Copy CodeThe code is as follows:
TMP = []
For LST in Names:
For name in LST:
If Name.count (' E ') >= 2:
Tmp.append (name)
Print tmp
#输出结果
>>>
[' Jefferson ', ' Wesley ', ' Steven ', ' Jennifer ']
Implemented with nested lists:
Copy CodeThe code is as follows:
>>> names = [' Tom ', ' Billy ', ' Jefferson ', ' Andrew ', ' Wesley ', ' Steven ', ' Joe ',
[' Alice ', ' Jill ', ' Ana ', ' Wendy ', ' Jennifer ', ' Sherry ', ' Eva ']
>>> [name for LST in names for name in LST if Name.count (' E ') >=2] #注意遍历顺序, which is the key to implementation
[' Jefferson ', ' Wesley ', ' Steven ', ' Jennifer ']