import functools from functools import reduce # Map () is built python higher order function, a receiving function and a function list # sequentially to the function f acts on each element of the list to obtain a new list and returns. Example # 1 each element of the list and returns the square do DEF F (X): return X * X Print ( List ( Map (F , [ . 1 , 2 , . 3 , . 4 , . 5 ]))) Print ( List ( Map ( the lambda X: X * X , [ . 1 , 2 , . 3 , . 4 , . 5 ]))) # anonymous function using the lambda # Note: because the list can contain any type of element, so that map () can not only handle the list contains values # list may further comprise any type of process, as long as the function f can be passed to process the data type # reduce () function is also built python higher-order functions, received parameters and map () is similar to a function f and a list, but behaves differently # reduce incoming f must receive two parameters, each element of a list of repeatedly calling the function f, and returns the final result value Example # 1 for all the elements of the list are summed DEF F (X , Y): return X + Y reduce(f, [1, 2, 3, 4, 5]) # May also receive the reduce the optional third parameter, as the calculation of the initial value # 2 Example summing all elements in list, and add 100 DEF F (X , Y): return X + Y reduce(f, [1, 2, 3, 4, 5], 100) # Filter functions are built python Another useful higher order function, it receives a function f and a list, the determination function f for each element # returns True or False, filter to automatically filter out according to the determination result does not meet the conditions element, and returns the elements eligible new list # 1 is deleted from the list in the embodiment even, odd retained DEF isOdd (X): return X% 2 == . 1 filter (isOdd , [ . 1 , 2 , . 3 , . 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 ]) # None Example 2. Remove empty string or DEF isNotEmpty (S): return S and len (s.strip ())> 0 filter (isNotEmpty , [ 'Test' , None, '' , '' ]) # Sort () is a higher order function, a comparison function may be received to implement custom ordering, comparison function is defined: Comparison with passing two elements X, Y # if x <y returns -1, x> y returns 1, x = y 0 is returned # Example 1. sort list the sorted ([ . 3 , . 6 , 2 , . 7 , 38 is , . 9 ]) # 2. Example if we are to achieve reverse sort, only a write their own reversedCmp function DEF reversedCmp (X , Y): IF X> Y: return - . 1 IF X <Y: return . 1 return 0 # the sorted ([5,3,6,4,1,7], reversedCmp) does not have to python3 # support the Print ( sorted ([ 'Bus' ,'amount','Zero','Ceiling'], key=None)) # Decorator DEF fprint (F): # decorator function DEF Fn (X): Print ( 'Call' + F. The __name__ + "()" ) return F (X) return Fn def f1(x): return x * x F1 = fprint # (F1) # equivalent @fPrint DEF F1 (X): return X * X Print (f1. __name__ ) # can see the function name is not 'f1', but 'fn' @fPrint internally defined # So for those who rely on the code name of the function will fail, and decorator also changes the function of __doc_ _ other property # If you want the caller can not see through the @decorator a function of "reform", you need to copy some of the attributes of the original function to a new function DEF fprint (f): # decorator function @functools. Wraps (F) DEF Fn (X): Print ( 'Call' + F. the __name__ + "()" ) return F (X) return Fn @fPrint DEF F1 (X): return X * X Print (F1. the __name__ ) Such function name becomes # 'f1' the Print (F1 ( 2 )) int ( '1010101' ) # default decimal INT2 functools.partial = ( int , Base = 2 ) new parameter set # using the partial function of binary function INT2 ( '1010101' )
python, map, reduce, filter, custom sorting function, function and partial function decorator
Guess you like
Origin blog.csdn.net/Trisyp/article/details/78832303
Ranking