current position:Home>Python efficient derivation (8)

Python efficient derivation (8)

2022-01-31 08:14:58 FizzH

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

Efficient derivation

List derivation

The grammatical form of list derivation is very simple , As shown below

[ Build expression for Variable in Sequence or iteration object ]

The outermost square brackets are the symbolic identity of the list , It indicates that the result of this expression is to generate a list , Therefore, it is called list derivation (list comprehensions). Functionally , The list derivation described in square brackets is equivalent to a loop , It's just that the form is more concise .

below , Let's use a few more examples to illustrate the power of list derivation

1. Filter unqualified elements in the original sequence

In list derivation , We can go through if Logical judgment of statements , Select the elements that meet the criteria . for example , If we want to extract integers from a list , And square it ,

aList = []
for x in range(4):
    aList.append(x**2)
 Copy code 

2. Use list derivation to tile nested lists

In the previous list derivation example , We only use one layer for Loop to generate a new list . in fact , We can also use two layers for loop . The following code uses two layers for An example of a loop tiling a nested list into a list .

vec = [[1,2,3],[4,5,6],[7,8,9]]
flat_vec = [num for elem in vec for num in elem]
print(flat_vec)

 Copy code 

Get the results

[1,2,3,4,5,6,7,8,9]

In the previous list derivation example , We only use one layer for Loop to generate a new list . in fact , We can also use two layers for loop . The following code uses two layers for An example of a loop tiling a nested list into a list .

3. Construct a specific list by combining multiple conditions

As mentioned earlier , The list derivation contains a pair of parentheses , There is an output expression in parentheses , The expression is followed by for sentence , And then there was 0 One or more for sentence 、if sentence , Through various combinations , Be able to construct all kinds of high-order lists . for example , The following list derivation combines the elements of two different lists .

new_list = [[x,y] for x in [1,2,3] for y in [3,1,4] if x!=y]
print(new-list)
 Copy code 

Get the results

[(1,3),(1,4),(2,3),(2,1),(2,4),(3,1),(3,4)]

It should be noted that , If the expression is a tuple , Such as In [12] Situated (x,y), Then you have to put parentheses on it .

4 Dictionary derivation

Dictionary derivation and list derivation are used in a similar way , It's just a sign of the list —— A square bracket [], Change to the tag of the dictionary —— A pair of curly braces {}. Illustrate with examples , The function of the following code is to exchange the keys and values of the original dictionary .

mcase = {'a':10,'b':30,'c':50}
kv-exchange = {v: k for  k, v in mcase.items()}
print(kv_exchange)
 Copy code 

Get the results

{10:'a',30:'b',50:'c'}

Of the above code In [2] Where a dictionary is used items() Method , It will return a list that supports traversal operations , In the list are things like ( key 0, value 0)、( key 1, value 2) Such a small tuple .

copyright notice
author[FizzH],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201310814552517.html

Random recommended