Map: Accepts two parameters (function, iterable), map passes the incoming function to each element of iterable, and returns the new iterable
def f (x): return x*= map (f,[1,2,3,4]) # at this time R is lazy evaluation-can be used next () and for...in value # return all through list () Print (List (R)) # [1, 4, 9, +]
Reduce: Accepts two parameters (function, sequence), reduce takes a function on a sequence, and the returned result continues to accumulate with the next element of the sequence, with the effect of: reduce (F,[x,y,z]) =>f (f (x, y), z)
from Import Reduce def Add (x, y ): return x+yreduce (add,[#
Filter: Filter sequence. Accept two parameters (function, sequence), filter to function on the sequence, depending on whether the return value is true, decide whether to discard the element
def is_odd (x): return x%2==1list (filter (is_odd,[1,2,3,4,5,6,7,8,9)) #Filter Lazy evaluation [1, 3, 5, 7, 9]
Sorted: Sort, sortable objects include number list, string list, dict, etc., can accept three parameters, the latter two are optional
# sorted ([],key=express,reverse=true) key: How to handle each element reverse: Reverse sort #[8, 6, 3,-2, 1,-1, 0]
Anonymous FunctionsLambda: anonymous function keyword: The preceding element represents the parameter of an anonymous function anonymous function without thanking the return expression, the return value is the value of the expression
m = List (map (Lambda x:x*x,[1,2,3,4))print(m) #[1, 4, 9, +]
Decorative Device: Dynamically increasing functionality during code run
#automatically print logs before the now function runsImportFunctoolsdeflog (func): @functools. Wraps (func)defWrpper (*args,**kw):Print("Call %s ()"% func.__name__) returnFunc (*args,**kw)returnWrpper#Call Adorner@logdefNow ():Print("Hello") Now ()#Call Now () Hello
Partial function: functools.partial Fixed some parameters of the function, returning a new function to make the call simpler
Import= functools.partial (int,base=2)print(Int2 ("1000000" )) #
Python-functional programming (passing functions as parameters)