For simple functions, there is also an easy way to represent a lambda expression
#普通函数
1def func (a):2 return a+13print' TEST1_FUNC0:', func (+)
4
#lambda表达式 5Lambda a:a+16print'test2_func0: ', func0 (+)
In this way, the results of the 1000+1 will be printed out of this function, but with the following
The existence of a lambda means a concise representation of a simple function.
Speaking of Lambda, here are some more small partners that can give Lambda buff:
1.map function, we use the map function to manipulate all the elements in the list. Map has two parameters (function, list), which iterates through each element of the list and executes the passed function arguments. In the output to the new list.
1 li = [one, one, 2] new_list = map (lambda a:a + li)
Output: [111, 122, 133]
Of course, map can also complete the addition of multiple arrays:
1 li = [one, one, one]2 sl = [1, 2, 3]3 new_list = map (Lambda A, b:a + B, Li, sl) 4 Print new_list
Output:[approx.]
2.reduce function, which accumulates for all elements in the sequence:
1 lst = [11,22,33]2 func2 = reduce (lambda arg1,arg2:arg1+arg2,lst)3 print'func2:', Func2
Output:func2:66
3.filter function, he can filter the data according to the conditions:
1 li = [one, one, 2] new_list = filter (lambda arg:arg > P, li)
New_list
Output: [33]
Lambda expression application in Python