1. Higher-order functions Map/reduce:Map: Accept two parameter map (f, list), perform F function operations on each element in the list, generate a Map object, call with loop or list type conversion.
def f (x): Return
x*x
a = map (f, [1, 2, 3, 4, 5])
list (a)
# [1, 4, 9,,]
b = Map (str, [1, 2, 3, 4, 5]
list (b)
# [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']
Reduce:reducce (f, list) sequence performs function f, equivalent to
Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)
Implement sum:
From Functools import reduce
def add (x, y): return
x + y
reduce (add, [1, 3, 5, 7, 9])
Filter
Return a true or false to each value in the list to decide whether to keep:
def is_odd (n): Return
n% 2 = 1
list (filter (is_odd, [1, 2, 4, 5, 6, 9, 10, 15])
Sorted
Sort ([list]) to accept a key= parameter custom sort function that will perform key operations on each item in the list, sorted according to the return value of the operation.
# sorted by absolute value
sorted ([5, -12, 9, -21], key=abs)
2. Return function
? The return value is a function that does not execute immediately, and then executes when you call.
def lazy_sum (*args):
def sum ():
ax = 0
for n in args:
ax = ax + n return AX-return
sum
l = [1, 2,3,4]
f = lazy_sum (*l)
f ()
# 10
The return function cannot call a loop variable because the return function does not execute immediately, and if the variable changes in execution the result of the return function is affected. If you must use loops to write this:
# a square of 1 to n, the calculation function exists in the list
def f (n):
def tmp (j):
def g (): Return
j*j return
g
fs = []
for I in Range (n):
fs.append (tmp (i)) return
FS
F1, f2, F3 = f (3)
F3 ()
# 4
3. anonymous function
Using a lambda does not write a function, the advantage is that the code is concise, do not worry about the function name repeat lambda [argument list]: [Returned expression]:
Reduce (lambda x, y:x + y, [1,2,3,4])
4. Decoration Device
If we want to do something before we start the function, and we don't want to change the form of the function, we can use an adorner, there are two forms, no adorner to pass in the parameter, and an adorner to pass in the argument:
1 adorners that do not pass in parameters:
Import functools
import time
def log (func): # Accept function
@functools. Wraps (func) # modifies the function name, Without this now function name will become wrapper
def wrapper (*args, **kw):
print (' Call%s (): '% func.__name__ "#添加操作 return
func (* args, **kw)
return wrapper # Returns a function that returns a function that has been added
@log
def now ():
Print (Time.localtime ( Time.time ()))
2 adorners for incoming parameters:
Import functools
import time
def log (text): # function
def decorator (func) to pass parameters: # Accept the function
you want to change @functools. Wraps (func) # Prevent function names from being changed
def wrapper (*args, **kw): # Accept the original function argument
print ('%s%s (): '% (text, func.__name__) #添加操作
return func (*args, **kw)
returns wrapper
returned decorator # returning modified function
@log (' Hellow World ")
def Now ():
print (Time.localtime (Time.time ()))
5. Partial function
When you want to call a function frequently, and the incoming parameter is different from the default parameter, you can use the partial function to create a new function and redefine its default value.
For example, the default parameter for int () is 10, and the passed-in variable is decimal, and if you want to pass in a number of 2, you can write:
Import functools
int2 = functools.partial (int, base=2)
int2 (' 10101011 ')
# 171