The map () function receives two parameters, one is a function, the other is a sequence, and map functions the passed-in function to each element of the sequence and returns the result as a new list.
>>> s = [' Aasda ', ' Dendy '] >>> def formatstr (ss): Return Ss[0].upper () + ss[1:len (ss)].lower () >> ;> v = formatstr (' Aaab ') >>> V ' aaab ' >>> map (formatstr, s) <map object at 0x000000000320a668> >>> for V in map (Formatstr, s): print (v) AASDA Dendy
Reduce functions a function in a sequence [X1, x2, x3 ...] , this function must receive two parameters, reduce to continue the result and the next element of the sequence cumulative calculation, the effect is:
Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)
>>> L = [2, 3, 4] >>> def prod (a, b): Return a * b >>> >>> functools Import * The;>> reduce (prod, L) >>> filter function, which is the same as map, is designed to filter eligible elements in the list:
>>> s = [1, 2, -3,-5] >>> def notLt0 (x): return x >= 0 >>> filter (notLt0, s) <filter o Bject at 0x000000000320a940> >>> with V in filter (notLt0, s): Print (v) 1 2 >>> as the example above, define a notLt0 method that method to determine whether an element is greater than or equal to 0, returns True, or false. Filter calls the NotLt0 method with each element of list s as a parameter, and then assembles all elements that return true to be returned as a list. anonymous function: You do not need to define a function's name in the following format:
fn = lambda function argument list: The function body where FN is the returned anonymous function, for example:
' anonymous function ' ' F = the closure of the lambda x:x * x print (f (2)) function, like a JavaScript closure, where an external function returns a reference to an intrinsic function that may hold a reference to an external function variable, for example:
' Closure ' def COUNT (): FS = [] for I in range (1, 4): Def f (): print (i) return I *