Lambda expressions can be understood as an abstract function implementation method, which has only the most basic three steps: give the argument, implement the expression, and return the result. This method is very clean, reduce the use of memory, the entire program is less pollution of the function, the code format will be more concise. However, the use in Python is limited and can only be evaluated in simple expressions.
Here's a quick example of a code that knows how to chop:
Func = (lambda for in xrange (4)) for in func: I (4 )141664
The meaning of the above code is simple, for I in xrange (4) Returns a list [0,1,2,3], and the Func object is actually an iterator iterator, after which the result is returned:
( Lambda x:x**0)(lambda x:x**1) (lambda x:x**2)(lambda x:x**3)
The lambda expression is then evaluated.
Other words:
I (0) = (lambda x:x**0) I (1) = (lambda x:x**1) I (2) = (lambda x:x**2 ) I (3) = (lambda x:x**3)
In the end, it naturally returns to the 1,4,16,64 such a result.
Another example of using the Map,filter function in conjunction with a lambda function:
>>> l = [' foo ', ' Bar ', ' far ']>>> map (lambda x:x, L) [' foo ', ' Bar ', ' far ']>>> filter (Lambda x: ' F ' in X, L) [' foo ', ' far ']>>> map (Lambda X:x.upper (), Filter (lambda x: ' F ' in X, L)) [' Foo ', ' far ']
Map (A, B) establishes a mapping relationship between a A and a, a is actually an element in B, and the lambda function evaluates the expression with a as a parameter before returning the result.
The same is true for filter, except that filter returns only the elements that the lambda function returns as true.
Properties of the lambda function