Lambda functions
Before we start, let's mention the lambda function. You can use the syntax of a lambda function to define a function. The lambda example is as follows:
Func = lambda x,y:x + y
Print func (3,4)
Lambda generates a function object. The function parameter is x, Y, and the return value is x+y. The function object is assigned to Func. The invocation of Func is the same as a normal function.
The above definitions can be written in the following form:
def func (x, y): return x + y
Map () function
Map () is a python built-in function. Its first argument is a function object.
Re = map ((lambda x:x+3), [1,3,5,6]) //result for [4,6,8,9]
Here, map () has two parameters, one is a function object defined by lambda, and the other is a table with multiple elements. the function of map () is to function objects sequentially on each element of the table, and the result of each action is stored in the returned table re . Map uses the read-in function (here is the lambda function) to manipulate the data (where "data" is each element of the table, and "operation" is 3 per data).
In Python 3.X, the return value of map () is a looping object. You can use the list () function to convert the loop object into a table.
If the function object as a parameter has more than one parameter, you can pass multiple parameters of the function parameter to map () using the following method:
Re = map ((lambda x,y:x+y), [1,2,3],[6,7,9])
Map () takes one element from each of the two tables at a time and brings it into the function defined by the lambda.
Filter () function
The first parameter of the filter function is also a function object. It is also a function object that will act as a parameter to multiple elements. If the function object returns True, the elements of that secondary are stored in the returned table. Filter filters the data by a function that is read in. Similarly, in Python 3.X, filter returns not a table, but a loop object.
The filter function uses the following example:
def func (a): if a >: return True else: return falseprint filter (func,[10,56,101,500])
Reduce () function
The first parameter of the reduce function is also a function, but there is a requirement that the function itself can receive two parameters. Reduce can accumulate functions on individual parameters. The following example:
Print reduce ((lambda x,y:x+y), [1,2,5,7,9])
The first parameter of reduce is a lambda function, which receives two parameters x, Y, and returns X+Y.
Reduce passes the first two elements (1 and 2) in a table to a lambda function, resulting in 3. The return value (3) is used as the first parameter of the lambda function, and the next element in the table (5) acts as the second parameter of the lambda function, making the next call to the lambda function, resulting in 8. Call the lambda function in turn, each time the first parameter of the lambda function is the result of the last operation, and the second argument is the next element in the table, until there are no remaining elements in the table.
The above example, equivalent (((1+2) +5) +7) +9
python--function