Python high-order function use summary! Understanding Python data structure and improving data quality (1)

Time:2021-4-9

The text and pictures of this article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time

The following article is from Tencent cloud by Yu Liang

在这里插入图片描述

This paper explains in detail the use of five built-in high-order functions in python with various practical examples, which can help understand Python’s data structure and improve the efficiency of data processing

  • map
  • reduce
  • filter
  • sorted/sort
  • zip
    在这里插入图片描述

1、 Map

在这里插入图片描述
在这里插入图片描述

1.2 demo

The use of map function is illustrated by examples

Using Python built-in functions
在这里插入图片描述
Using custom functions
在这里插入图片描述
Using the anonymous function lambda
You can use anonymous functions with multiple arguments
在这里插入图片描述

2、 Reduce

在这里插入图片描述
在这里插入图片描述
The above example is explained by a graph
在这里插入图片描述

2.3 demo

Using custom functions
在这里插入图片描述
Using the anonymous function lambda
在这里插入图片描述
image-20201024185550970

A complex example
在这里插入图片描述
The specific process is as follows:

1. 1*2+1=3
2. 3 * 3 + 1 = 10 # the first 3 is the result 3 above, and the second 3 in the original data
3. 10*4+1=41

Examples with initial values
The initialization value and the first value in the sequence execute the func function and take the result as the starting value of the next time
在这里插入图片描述

#The specific process is explained as follows

1. 6+1=7  
2. 7+2=9
3. 9+3=12
4. 12+4=16
5. 16+5=21

3、 Filter

在这里插入图片描述
在这里插入图片描述
Using custom functions
Returns an even number within 10
在这里插入图片描述
Using the anonymous function lambda
在这里插入图片描述
Filtering strings
Select a string that meets the specified requirements
在这里插入图片描述

Recommended Today

Less sass SCSS stylus of CSS preprocessor

Let’s ask ourselves a question: why do we need a preprocessor?The answer is self-evident, that is, CSS itself has some disadvantages: The syntax is not strong enough to be written nested, resulting in unclear style logic in the project. There is no variable and logical reuse mechanism. When it is necessary to reuse code, you […]