This article describes how to use the derivation in Python. This article describes the list derivation, Dictionary derivation, and set derivation examples, if you need it, you can refer to the derivation below, which is a very powerful and popular feature in Python and has the advantages of concise language and fast speed. The Derivation includes:
1. List Derivation
2. dictionary Derivation
3. Set Derivation
Nested list Derivation
NOTE: dictionary and set derivation are recently added to Python (Python 2.7 and Python 3.1 or later). The following is a brief introduction:
List Derivation]
List derivation can easily construct a new list: only a concise expression can be used to convert and deform the elements.
The basic format is as follows:
The Code is as follows:
[Expr for value in collection ifcondition]
The filtering condition is optional, depending on the actual application, leaving only the expression; equivalent to the following for loop:
The Code is as follows:
Result = []
For value in collection:
If condition:
Result. append (expression)
Example 1: filter out the string list with a length less than 3 and convert the remaining string to uppercase/lowercase letters.
The 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: Calculate (x, y) where x is an even number between 0 and 5, and y is an odd number between 0 and 5.
The 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: List composed of 3, 6, and 9 in M
The Code is as follows:
>>> M = [[1, 2, 3],
... [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]
# Or use the following method
>>> [M [row] [2] for row in (0, 1, 2)]
[3, 6, 9]
Example 4: List composed of a diagonal line 1, 5, and 9 in M
The 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: Calculate the product of the matrix and element in M and N.
The Code is as follows:
>>> M = [[1, 2, 3],
... [4, 5, 6],
... [7, 8, 9]
>>> N = [[2, 2],
... [3, 3],
... [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: Describe the age key in the dictionary and add new values according to the conditions.
The Code is as follows:
>>> Bob
{'Pay': 3000, 'job': 'dev', 'age': 42, 'name': 'Bob Smith '}
>>> Sue
{'Pay': 4000, 'job': 'hdw', 'age': 45, 'name': 'sue Jones '}
>>> People = [bob, sue]
>>> [Rec ['age'] + 100 if rec ['age']> = 45 else rec ['age'] for rec in people] # Pay Attention to the for Location
[42,145]
[Dictionary derivation]
The dictionary and the Set derivation are the continuation of this idea. The syntax is similar, but only the set and the dictionary are generated. The basic format is as follows:
The Code is as follows:
{Key_expr: value_expr for value in collection if condition}
Example 1: Use dictionary derivation to create a dictionary with a string and its length
The 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 derivation is very similar to the list derivation. The only difference is that {} is used instead of []. The basic format is as follows:
The Code is as follows:
{Expr for value in collection if condition}
Example 1: Use a set to derive the string length set
The Code is as follows:
>>> Strings = ['A', 'is ', 'with', 'if', 'file', 'exception']
>>>{ Len (s) for s in strings} # If the length is the same, only one is left. This is also very useful in fact.
Set ([1, 2, 4, 9])
[Derivation of nested list]
The nested list refers to the nested list in the list, for example:
The Code is as follows:
>>> L = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
Example 1: A nested list composed of a list of men and women. The list is composed of names with more than two letters e.
The Code is as follows:
Names = [['Tom ', 'Billy', 'jefferson ', 'Andrew', 'westsley ', 'steven', 'job'],
['Alice ', 'jill', 'ana', 'wendy ', 'jennifer', 'sherry', 'eva ']
Implement with a for Loop:
The Code is as follows:
Tmp = []
For lst in names:
For name in lst:
If name. count ('E')> = 2:
Tmp. append (name)
Print tmp
# Output result
>>>
['Jefferson ', 'westsley', 'steven ', 'jennifer']
Implement with nested list:
The Code is as follows:
>>> Names = [['Tom ', 'Billy', 'jefferson ', 'Andrew', 'westsley ', 'steven', 'Joe '],
['Alice ', 'jill', 'ana', 'wendy ', 'jennifer', 'sherry', 'eva ']
>>> [Name for lst in names for name in lst if name. count ('E') >=2] # Pay Attention to the traversal order, which is the key to implementation
['Jefferson ', 'westsley', 'steven ', 'jennifer']