Python Learning (5) functions-built-in functions lambda filter map reduce, pythonlambda
Python built-in functions lambda, filter, map, reduce
Python has built-in special and practical functions that make your code concise and easy to read.
Python lambda, filter, map, and reduce. Reduce only mentions that recursive methods are recommended to be replaced by loops.
Lambda anonymous Functions
In a lambda statement, there are multiple parameters before the colon, which can be separated by commas (,). return values on the right of the colon.
Lambda statements are actually built as a function object. refer to the following example to feel the lambda anonymous function:
1 def f (I): # Number of custom returned records 2 return I * i3 print (f (3) 4 5g = lambda x: x * x # lambda anonymous function 6 print (g (3 ))
Compared with normal functions, lambda functions save the function name. At the same time, such anonymous functions cannot be called in other places, and many other methods can replace lambda.
Is lambda unnecessary? Lambda has the following advantages:
1. The process of defining functions is omitted, and the minimum function of a single row is quickly defined to simplify the code.
2. functions that do not need to be reused and do not need to be defined.
3. Combined with filter map reduce
Lambda functions can contain multiple parameters. See the following example:
1 def f(x,y):2 return x+y3 print(f(3,4))4 g = lambda x,y : x+y5 print(g(3,4))
Lambda is similar to a macro in C language and borrowed from LISP. C #3.0 also has a similar expression. The keyword is =>. For details, see:
1 var array = new int[] {2, 3, 5, 7, 9};2 var result = array.Where(n => n > 3); // [5, 6, 9]
Filter Function
The filter function filters the specified sequence. filter (function or None, sequence)-> filter object (list, tuple, or string in 2.x)
The filter function calls the function for each element in the sequence parameter. The final returned result contains the element whose call result is True.
Note: 3.x returns a filter object (the subsequent Function map also returns the object), which must be converted by list () and Other types. The type of the returned value in the previous 2.x version is the same as that of the sequence parameter.
1 '''filter''' 2 def is_Even (I): 3 if (I % 2 = 0): 4 return True 5 else: 6 return False 7 l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 8 l = filter (is_Even, l) 9 print (l) # 3.x returns the object filter object10 l = list (l) # 3.x requires type conversion 11 print (l) 12 13 l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 14 l = list (filter (lambda x: x % 2 = 0, l )) # combined with lambda15 print (l)
Map Functions
The map function maps the specified sequence. map (function or None, sequence)-> map object (list, tuple, or string in 2.x)
The map function calls the function for each element in the sequence parameter. The returned result is the return value of the function called by each element.
1 '''map''' 2 def sqr (I): 3 return I ** 2 4 l = [1, 2, 3] 5 l = map (sqr, l) 6 print (l) # 3.x returns the object map object 7 l = list (l) 8 print (l) 9 10 l = [1, 2, 3] 11 l = list (map (lambda x: x ** 2, l) # combined with lambda12 print (l)
Reduce Function
Note: The reduce function is not a built-in function in version 3.x. The reduce () function has been removed from the global namespace and is now placed in the fucntools module.
From functools import reduce
The reduce function performs iterative operations on the specified sequence. reduce (function, sequence [, initial])-> value
The function parameter in the reduce function is a function with two parameters. reduce extracts an element from sequence sequentially and calls the function again based on the result of the last function call.
1 '''reduce' 2 from functools import reduce # 3.x Version 3 def sum (x, y): 4 return x + y 5 l = [1, 2, 3, 4, 5, 6] 6 l = reduce (sum, l) 7 print (l) 8 9 l = [1, 2, 4, 5, 6] 10 l = reduce (lambda x, y: x + y, l) # combined with lambda11 print (l) 12 help (reduce) # view reduce help