Chapter 3 python3, python3
Derivation (also known as 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
List derivation provides a simple method to generate a list. A common application is to create a new list, where each element is the result of some operations applied to each member of another sequence or iteratable, or create subsequences of elements that meet specific conditions.
The basic format is as follows:
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. The filtering conditions are optional, depending on the actual application.
For example, suppose we want to create a list of shards, for example:
squares = []for x in range(10): squares.append(x**2)print(squares)
The above code outputs:
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Note that the above created (or overwritten) a variable named x, which still exists at the end of the loop. We can calculate the list of workers without any side effects:
squares = list(map(lambda x: x**2, range(10)))
Or, equivalent:
squares = [x**2 for x in range(10)]
The above method is more concise and easy to read.
The list derivation is composed of a pair of square brackets. Square brackets contain an expression followed by a for clause, followed by zero or more for or if clauses. The result will be a new list with values derived from the results that evaluate the expression in the context of the for and if clauses. For example, if the two lists have different elements, the listcomp combines the two list elements:
squares = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]print(squares)
It is equivalent:
squares = [(x, y) for x in [1, 2, 3] for y in [3, 1, 4] if x != y]for x in [1, 2, 3]: for y in [3, 1, 4]: if x != y: squares.append((x, y))print(squares)
Note how the order of the for and if statements is consistent between the two code snippets. The above code outputs:
[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4), (1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
Change the deduced [] to () to get the generator:
squares = (x**2 for x in range(10))print(type(squares))
The above code outputs:
<class 'generator'>
The first expression of list derivation can be any expression, including another list derivation, which we call nested list derivation. Instance:
# Example of a 3 × 4 matrix, which is implemented as a list of three lists with a length of 4 matrix = [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],] # The following list derives the transpose row and column res = [[row [I] for row in matrix] for I in range (4)] print (res)
The above code outputs:
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]
The nested list derivation evaluates the value in the for context following it, so this example is equivalent:
# Example of a 3 × 4 matrix, which is implemented as a list of three lists with a length of 4 matrix = [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],] # The list derivation below will transpose rows and columns transposed = [] for I in range (4 ): transposed. append ([row [I] for row in matrix]) print (transposed)
Think: (x, y) where x is an even number between 0 and 5, and y is a list of tuples composed of odd numbers between 0 and 5. The following code is provided, but try to write it out first:
Res = [(x, y) for x in range (5) if x % 2 = 0 for y in range (5) if y % 2 = 1] print (res)View Code
2. 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:
{ key_expr: value_expr for value in collection if condition }
Instance to quickly change the key and value:
data = {'a': 10, 'b': 34, 'c':100}res = {v: k for k, v in data.items()}print(res)
The above code outputs:
{10: 'a', 34: 'b', 100: 'c'}
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:
{ expr for value in collection if condition }
Instance:
squared = {x**2 for x in [1, 1, 2]}print(squared)
The above code outputs:
{1, 4}