Everything in Python is an object, and a function is also considered an object.
several built-in functions are commonly used in Map,reduce, Filter,lamda.
Filter
Filter out elements that meet the filter criteria
s = "@431$%$314&6i7 (431) ^&^%2l#%^i6861642k765e&$%65%^$^# $p%^&*%66757y%*^&%th%*&^%&^$ o$&*^n4637 "Print filter (str.isalpha,s)
Map
Receive two parameters, the first is a function, the second is a list, and the result is returned
L = [Random.randint (0,100) for I in range (Ten)]def Sub50 (a): return a-50print map (sub50,l)
Reduce
Receive two parameters as a map, but he returns a result that iterates over the second parameter list and calculates it continuously.
# 1+2+3+...+99def F_add (A, B): return a+bprint reduce (F_add,range (1,100))
In Python3, reduce is removed from the global namespace, and in order to use the--from functools import reduce, the concept expressed in the function of reduce is called the Statute fold.
Map reduce and big data in this case are not the same thing.
Lambda
Lambda is an anonymous function that uses a lambda when it needs a function, but does not bother to name it.
Map (Lambda x:x*x, [x for x in range]) # #等价于def PF (x): return x * XMAP (PF, [x for X in range (10)])
Map,filter,reduce No list parsing is easy to understand, 3 returns a iterator and is no longer a list.
The callback CallBack is also the function as a parameter, running to CallBack () to return to the previous layer
Closed Bag closure
Functions that bind external variables
Closures and adorners are functions as return values
def pow_x (x): def Echo (value): return value**x return echolst = [Pow_x (2), pow_x (3), pow_x (4)]for p in lst:< C5/>print "Closure", p (2)
1. Nesting functions
2. External variables are used for intrinsic functions
3. The external function returns the intrinsic function
If the external variable is list, it can be changed. But it's just a variable that can't be changed.
Inside the function, be aware of the LEGB principle, when there are local variables, the external variables are no longer called.
Adorner decorator
Decoration is on the basis of the original function, plus some other functions, he is the function as a return value, function as an application of parameters.
Multi-layer nesting is the use of closures, modified function as binding quantitative.
Two-tiered nesting structure
The target function can take parameters
Def time_cost (f): def _f (*arg, **kwarg): start = Time.clock () a=f (*arg,**kwarg) end = Time.clock () print f.__name__, "Run cost time is", End-start return a return _f @time_costdef list_comp (length): return [(x, Y) for x in range (length) for y in range (length) if x*y > 25]if __name__ = = ' __main__ ': list_comp (1 000)
Three-layer nested structure
Not only the target function can take parameters, but the adorner can also take parameters
Recursive
Recursion is 1) The function itself calls itself 2) has a clear exit. It is commonly used in the Fibonacci sequence, the traversal of the tree graph, etc., or the structure is defined as recursion. Recursion and looping are equivalent.
Too many recursion times can cause stack overflow, and Python does not support tail recursion optimization.
#字符串取反def Reverse_str (s): If Len (s) <= 1: return s else: return Reverse_str (s[1:]) + s[0]
Python notes (2) function