Author: vamei Source: http://www.cnblogs.com/vamei welcome reprint, please also keep this statement. Thank you!
Adhering to the idea that everything is an object, let's look back at the function structure. A function is actually an object. Since it is an object, it also has attributes (which can be queried using Dir ). As an object, it can also be assigned to other variable names or passed to other functions as parameters.
1. Lambda
Before proceeding, we should first mention lambda. Lambda is easy to use.Define functionsMethod,Its functions can be fully defined by def.. Lambda example:
Func =LambdaX, Y: x + y
Print func (3, 4)
Lambda and subsequent content actually generate a function object (that is, a function ). The return value is X + Y. This function object is assigned to the function name func. The call of func is no different from that of a normal function.
The preceding definition can be written as follows:
DefFunc (x, y ):ReturnX + Y
2. functions can be passed as parameters.
A function can be passed as an object.. The function name (such as func) points to this object without parentheses. For example:
DefTest (F, a, B ):Print 'Test'PrintF (a, B) test (func,3, 5)
We can see that the first parameter F of the test function is a function object. If we pass func to F, then f () in test is actually implemented by func.
In this way, we have greatly improvedProgramFlexibility. Suppose we have another function to replace func, and we can use the same test function. As follows:
Test ((LambdaX, Y: X ** 2 + Y), 6, 9)
Think about the meaning of this program.
3. Map Functions
Map () is a python built-in function. Its first parameter is a function object.
Re = map ((LambdaX: x + 3), [1, 3, 5, 6])
Here, map () has two parameters: a function object defined by Lambda and a table containing multiple elements.The function of map () is to act function objects on each element of the table in turn.The results of each action are stored in the returned table re. Map uses the READ function (here is the lambda function)OperationData (here "data" is each element in the table, and "operation" is to add 3 to each data ).
(Note: In Python 3. X, map () will output the yield result each time to form a circular object. You can use the list () function to convert the circular object into a table)
If the function object as a parameter has multiple parameters, you can use the following example:
Re = map ((LambdaX, Y: X + Y), [1, 2], [6, 7, 9])
Map () extracts one element from two tables each time and brings it into the function defined by Lambda.
(Lambda used in this section can also be a more complex function defined by def)
4. Filter Function
The filter function is similar to the map function, and acts as a function object as a parameter on each element of the table.If the return value of the function object is true, the element is stored in the returned table.Filters are read by functions.FilterData.(Similarly, in Python 3. X, filters return loop objects instead of tables .)
The filter function is used as follows:
DefFunc ():IfA> 100:ReturnTrueElse:ReturnFalsePrintFilter (func, [101,500,])
5. Reduce Functions
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 canProgressively apply a function to each parameter. For example:
PrintReduce ((LambdaX, Y: X + Y), [1, 2, 5, 7, 9])
The first parameter of reduce is the lambda function, which receives two parameters X, Y and returns X + Y.
Reduce transfers the first two elements (1 and 2) of the table to the lambda function. 3 is displayed. The returned value (3) is used as the first parameter of the lambda function, and the next element (5) in the table is used as the second parameter of the lambda function, get 8. Call the lambda function in sequence. The first parameter of each Lambda function is the result of the previous operation, and the second parameter is the next element in the table until there is no remaining element in the table.
The above example is equivalent to (1 + 2) + 5) + 7) + 9
(According to the reminder of mmufhy: The reduce () function cannot be used directly in 3.0. It is defined in the functools package and needs to be introduced. See the comment area)
Summary:
A function is an object.
Define functions using Lambda
Map ()
Filter ()
Reduce ()