It's well-known for functions, but for some functions it's hard to understand, so let's take a look at some of the functions that Python comes with, before we ask you to understand what a higher-order function is.
Higher order functions: higher-order functions in functions
-
map () function
Format: Map (FUNC,SEQ)
return value: List
The function of the map function is to function func to each element in the SEQ, and the result of each element is stored in the list.
For example, we use the function func=x+x*x as an example for the sequence [1,2,3,4,5,6,7,8]
!/bin/pythondef func (x): return x+x*xlist=map (func,[1,2,3,4,5,6,7,8]) print ( List) Run result: [2,6,11,20,30,42,58,72]
-
Format: Filter (FUNC,SEQ) the
Filter function acts on every element of the sequence, as does the map function. But the difference is that filter will decide whether to keep or discard according to the return value True/false, so it is called filter function
For example: We have a function to find the element in a string that is lowercase
!/bin/pythondef func (x): return x.islower () str= ' JiJUHDEjideqHEUH ' List=filter (Func,list (str)) print (list) Run result: j[' i ', ' j ', ' I ', ' d ', ' e ', ' Q ']
-
Format: Reduce (FUNC,SEQ)
return value: The cumulative result of Func
The reduce function is different from the map and filter functions. It acts on the entire SEQ and accumulates the result and the next element block of the sequence.
For example: Sequence summation, which can be implemented in Python using the SUM function, but can also be implemented using the Reduce function
!/bin/pythondef sum (x, y): Return X+ycount=reduce (sum,[1,2,3,4,5,6,7,8,9,10]) print (count) Run Result:
For the above example is a simple use of a single function, down we combine the use of the above function.
For example: Find the number of daffodils in a sequence and add the number of daffodils
!/bin/pythondef Is_bai (x): Return x>100def is_flower (x): A=int (x/100) b=int ((x-a*100)/10) c=x-a*100-b*10 return a*a*a+b*b*b+c*c*cdef func1 (x, y): Return X+ylist=[12,123,153,170,372,371]sum=reduce (Func1,map (Is_flower, Filter (is_bai,list)))
-
An anonymous function is one that does not define a function with the DEF keyword, but instead uses lambda to create a function.
Format: Lambda parameter: return value
for an anonymous function, its body is not a block of code, but an expression, and we use an example to understand the lambda format.
For example: we ask for two yes and
The general situation we coded:
!/bin/pythondef sum (x, y): Return x+ysum (1,3) results are: 4
We use anonymous functions to complete:
!/bin/pythonc=lanbda X,y:x+yprint (c) The result is: 4
The colon (:) in the format is a delimiter
Invocation of anonymous functions:
Variable =LANBDA parameter: return value
Note : Python support for anonymous functions is limited, and anonymous functions can be used only in some simple cases.
-
The partial function is a function provided by the Functools module in the Python module, which can be a more concise code
Format: Partial (FUNCT,FUNCT_ARGX1, Funct_argx2 ...) the
partial function takes all the hosted functions as one parameter of the partial () function, and each parameter of the original function acts as a subsequent argument to the partial () function once, unless the keyword argument is used.
Function: Fix some parameters of a function (set default parameters) and return a new function.
Let's take an example to understand this format.
For example: We ask for a different number for 3 of the remainder
!/bin/pythondef mod (m,n) return m%nfunc_new=partial (mod,3) print (' Call custom Function, 100 to 3 remainder ', mod (100,3)) print (' Call partial function, 100 to 3 ", Func_new (3)) operation result is: Call the Custom function, 100 to 3 take the remainder 1 call the partial function, 100 to 3 take the remainder 1
For the partial function to set the default value of the Downgrade function call difficulty, and when the function of the number of arguments too much, need to simplify, use functools.partial can create a new function, the new function can be fixed in the original function part of the parameters, making it easier to call.
[Python] Learning Basics: The advanced chapter of functions