Python high-order function use summary! Understand Python data structure and improve data quality (2)

Time:2021-4-8

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

在这里插入图片描述
在这里插入图片描述
Do not reverse by default
在这里插入图片描述
Sort tuples, range objects and dictionaries
在这里插入图片描述
4.4 result reversal
The meaning of result inversion is to arrange the results in descending order, because the original default is ascending order, using reverse = true
在这里插入图片描述
4.5 understanding
The key parameter is used to customize a function, and then sort the elements in the sequence after acting on the function

Here we use the absolute value function
在这里插入图片描述
在这里插入图片描述
4.6 contrast sort ()
The sort () method can only sort the original list, and the parameters are the same as sorted

The result is to modify the original list directly in place, while sorted is to generate a new list. The two are different
在这里插入图片描述

5、 Zip

Zip () is a very important method in Python, which can realize many functions quickly.

5.1 grammar
在这里插入图片描述
在这里插入图片描述
5.2 zip accepts a sequence
Zip can accept list, tuple, string and other forms
在这里插入图片描述
Zip accepts the form of an empty list and returns an empty list
在这里插入图片描述
5.3 zip accepts multiple sequences
在这里插入图片描述
At the same time, different types of sequences are merged
在这里插入图片描述
5.4 different treatment length
When multiple sequences exist at the same time, the length of the sequence with the smallest length is taken
在这里插入图片描述
5.5 zip(*iterables)
We generally think that this method is the inverse process of zip and an unzip process
在这里插入图片描述
5.6 complex examples
Let’s take a more complicated example
在这里插入图片描述
在这里插入图片描述
List summation
在这里插入图片描述
Data merging
在这里插入图片描述
Key value conversion of dictionary
For loop implementation:
在这里插入图片描述
Using zip to achieve:
在这里插入图片描述