One 、 Higher order function
Function definition
The function name can be used as the return value 、 Can also be used as a parameter
(1) Function name as argument
(2) Function name as return value
Two 、 Common higher order functions
(1)map(function,iterable)
(2)filter(function, iterable)
(3)reduce(function, iterable)
One 、 Higher order function Function definitionpython in , Function names are variables , Below this method Function names are treated as variables , Point to a computed function ! So the function name is actually the variable that points to the function , So variables can point to functions ;
Variables can point to functions , And the variables of the function can accept variables , Then any function can accept a function as a parameter , This function is called higher order function
The function name can be used as the return value 、 Can also be used as a parameterdef method(a,b): return a+ba = methodprint(a(1,4))# Output ; 5
(1) Function name as argument def fun(a): return a*adef fun1(a,b,c): return a(b)+a(c)print(fun1(fun(),1,5)) # fun() As a parameter , Pass to fun1# Output :26
(2) Function name as return value def fun(a): def fun2(b): return a*b return fun2()a = fun(1) print(a) # The external function returns a function object , That is, a variable , Then pass parameters as function objects , Get the return value of the inner function print(a(3))# Output :<function method.<locals>.method2 at 0x02B5E9C0>6
Two 、 Common higher order functions map、filter、reduce
Let's talk about anonymous functions first lambda,
Grammar format :lambda[ Shape parameter 1, Shape parameter 2,... : expression ]
After the expression is executed , Returns the expression after the colon ;
x = 1lambda x:x+3
(1)map(function,iterable)Its first element to pass is the function name or lambda Anonymous function expression , The second one is the iteratable object ;
python 2.x Returns a list of ,python 3.x Return iterator
array = [1,4,7]a = map(lambda x:x+1,array)print(a)print(list(a)) Output :<map object at 0x02AB11B0>[2, 5, 8]
map() The function is used to perform function operations on each element in an iteratable object , Then the iteratable object formed by the element operation is returned to
(2)filter(function, iterable)Again , Its first element to pass is the function name or lambda Anonymous function expression , The second one is the iteratable object
array = [1,4,7]a = filter(lambda x:x+1,array)print(a)print(list(a)) Output :<filter object at 0x02AB11B0>[1,4,7]
array = [1,4,7]a = filter(lambda x:x%2==0,array)print(a)print(list(a)) Output :<filter object at 0x02AB11B0>[4]
filter function , Is to bring the elements of an iteratable object into a function , return True The object of , Generally used for filtering , Omit for loop
(3)reduce(function, iterable)It's about 2 Elements , Before the 2 Elements are brought into the function , Then take the return value as an element , And the third element , Until the end, the final result is returned .
reduce(function, iterable[, initializer]), Accumulate the items of a sequence from left to right
reduce() Function in python2 Is built-in function , stay python3 In the middle functools Under module :
from functools import reducearray = [1,2,3,4,5]result = reduce(lambda x,y:x+y,array)print(result)# result :151+2 = 33+3 = 66+4=1010+5=15
That's all python Details of the tutorial examples for using higher-order functions , More about python For information on higher-order functions, please pay attention to other relevant articles on the software development network !