Click **Machine Learning Algorithms and Python Learning, **Select Star
Wonderful content don’t get lost
Datawhale dry goods, author: Paper money super, Xiamen University
This article explains in detail the use of the 5 built-in higher-order functions of Python
combined with various practical examples, which can help understand the data structure of Python and improve the efficiency of data processing. The 5 functions are:
The basic syntax of map
function is map(func, seq)
, and its meaning means: execute the function of the previous function func
for each element in the iterable sequence, and finally obtain a new sequence. note:
Python2
directly returns a list What is returned in Python3
is an iterator, If you want to return a list, you can use list() to processhelp(map) #View help information
Illustrate how to use the map
function with examples
There can be multiple parameters when using anonymous functions
Definition of reduce
function:
reduce(function, sequence [, initial])-> value
reduce
takes an element from sequence
in turn, and uses the result of the last call to function
as a parameter, and calls function
again.
"When calling a function for the first time, if the initial parameter is provided, the function will be called with the first element in the sequence and initial as the parameters, otherwise it will be called with the first number of the sequence
”
In Python3
, the reduce
function has been moved to the functools
module, and it needs to be imported first:
from functools import reduce #Import
help(reduce) #View help documentation
In the above example, we use a graphic to explain:
image-20201024185550970
The specific process is:
1.1*2+1=32.3*3+1=10 # The first 3 is the result 3 above, and the second is 33 in the original data.10*4+1=41
The initial value and the first value in the sequence execute the func function, and the result will be the next starting value
# The specific process is explained as
1.6+1=72.7+2=93.9+3=124.12+4=165.16+5=21
The filter()
function is used to filter the sequence, filter out those elements that do not meet the conditions, and return the elements that meet the conditions to form a new list.
Each element in the sequence is passed as a parameter to the function for judgment, returns True or False, and finally the element that returns True is placed in the new list.
filter()
syntax is as follows:
filter(function, iterable) #The former is a function, the latter is a sequence to be executed
help(filter) #Help document
Return even numbers within 10
Select a string that meets the specified requirements
sorted(iterable, key=None, reverse=False)
sorted()
accepts 3 parameters, returns a sorted list
iterable
reverse=False
, accept a Boolean value, choose whether to reverse the sorting result, the default is False
key=None
, the callback function can only have one parameter, sorted according to the return value of the functionhelp(sorted) #Help document
The meaning of result reversal is sort the results in descending order, because the original default is ascending order, and reverse=True
is used.
The function of the key
parameter is that we customize a function, and then sort by applying the elements in the sequence to the function
Here we use absolute value function
The sort() method can only sort the original list, and the parameters are the same as sorted
The result is that the original list is directly modified in place, while sorted is to generate a new list, the two are different
zip()
is a very important method in Python
, which can quickly implement many functions.
zip([iterable,...]) #iterable is one or more iterators
tuple list
help(zip) #View document
zip
can accept lists, tuples, strings and other forms
zip
accepts the form of an empty list, and the returned list is still empty
Simultaneously merge different types of sequences
When multiple sequences exist at the same time, take the length of the smallest sequence
We generally think that this method is the inverse process of zip
, it is a process of unzip
, for example to illustrate its use:
Let’s look at a more complex example
The explanation of this example is:
[ x]
is a list containing lists, x
itself is a list[ x]*3
The result is [x,x,x]
, which is actually [[4,5,6],[4,5,6],[4,5,6]]
[*[ x]*3]
The result is [(4,4,4),(5,5,5),(6,6,6)]
The following is a practical example of zip
to illustrate its application:
For
loop implementation:
Use zip
to achieve:
**Click "Looking" if you like it! **
Recommended Posts