Higher order function: a function that passes a function as a parameter, such as
def Add (x,y,f): return f (x) + f (y)print(Add ( -8,11,abs))
Operation Result:
19
Note:
1. Call the Add function to perform ABS (-8) and ABC (11) respectively, and calculate their values separately
2, the last to do the operation
Map () function
The map () function is an advanced function built into python that takes a function f and a list and passes the list's elements to the function f, and then returns a function f that processes the list of all lists elements. For example
def F2 (x): return x*= [1,2,3,4,5]print(map (f2,l))
Operation Result:
[1, 4, 9, 16, 25]
Note: 1, L is a list, the elements of this list into the function F2, the square of each element
2. Combine the final calculation results into a list.
Reduce () function
The reduce () function is also a built-in high-order function, and the reduce () function receives a parameter similar to map (), a function f, a list, and a map (), and reduce () the incoming function f must receive 2 parameters, The first call is to pass the top 2 elements of the list to F, the second call, the calculation of the two list element as a parameter, the third element of the list as the second parameter, the introduction of the F operation, the next time and again, and return the final result.
def f (x, y ): return x + yprint(reduce (f,[1,2,3,4,5],10))
Operation Result:
25
Note: The above run procedure 1+2+3+4+5 the last 10 is a default value, which is finally executed once
The incoming function must accept 2 parameters
The first two parameters of the iterated object are used as the arguments of the function and passed into the function f
Use the result of each F operation as an argument, iterate over the next element of the object as another argument, and pass in the function f
An analogy, and finally get the result
Filter () function
The filter () function receives a function f and a list, the function of which is to judge each element, return True or False,filter () automatically filters out the non-conforming elements according to the result of the decision, and returns a new list that is composed of qualifying elements. Cases:
A = [1,2,3,4,5,6,7,8]def is_odd (x): return x%2 = = 1print(is_odd (5 ) )Print(filter (IS_ODD,A))
Operation Result:
true[1, 3, 5, 7]
Note:
1, the elements of the list are passed into the function is_odd
2, is_odd to determine whether each element meets the conditions, the conditions of the left, not meet the conditions of abandonment
3. Make a new list of all the elements that are ultimately eligible
Each time the function passes in an element that iterates over an object, and if it returns True, the element is preserved and the element is not preserved if returned to False
Sorted () function
Sorted (...)
Sorted (iterable, Cmp=none, Key=none, Reverse=false)--New sorted list
Sort the dictionary
Sort by value
DIC = Dict (a=1,b=10,c=3,d=9)
DIC = Dict (a=1,b=10,c=3,d=9)
Print (sorted (Dic.iteritems (), Key=lambda D:d[1],reverse=false))
Results: [(' A ', 1), (' C ', 3), (' d ', 9), (' B ', 10)]
Anonymous functions: Functions without a name
The lambda () function is a minimal function that quickly defines a single line and can be used wherever a function is needed
Common functions:
def Fun (x, y ): return x*Lambda x,y:x*yprint(R (2,3))print(type (R) )
Operation Result:
' function '>
(iii) 3-4 Python's higher order functions and anonymous functions