In the process of learning Python, the syntax of Lambda is often confusing, what is lambda, why use lambda, is it necessary to use lambda?
Here are answers to the above questions.
1. What is lambda?
Look at an example:
1 g = lambda x:x+1
Take a look at the results of the execution:
G (1)
>>>2
G (2)
>>>3
Of course, you can also use this:
Lambda x:x+1 (1)
>>>2
It can be thought that lambda, as an expression, defines an anonymous function, the code x for the example above is the entry parameter, x+1 is the function body, and is represented by a function:
1 def g (x):
2 return x+1
Very easy to understand, where Lambda simplifies the writing of function definitions. Code is more concise, but the use of functions is more intuitive and easy to understand.
In Python, there are also several well-defined global functions that are easy to use, filter, map, reduce
>>> foo = [2, 18, 9, 22, 17, 24, 8, 12, 27]
>>>
>>> Print filter (lambda x:x% 3 = = 0, foo)
[18, 9, 24, 12, 27]
>>>
>>> Print Map (Lambda x:x * 2 + ten, foo)
[14, 46, 28, 54, 44, 58, 26, 34, 64]
>>>
>>> print reduce (lambda x, y:x + y, foo)
139
The role of the map in the example above is very simple and clear. But does Python have to use lambda to make this simple? In the object traversal process, in fact Python for. In.. The IF syntax is already strong and is more readable than lambda.
For example, the map above can be written as:
print [x * 2 + ten for x in Foo]
It's very concise and easy to understand.
The filter example can be written as:
print [x for x in foo if x% 3 = = 0]
It's also easier to understand than a lambda way.
Here's a brief description of what Lambda is, and here's why you use lambda to see an example (from apihelper.py):
Processfunc = Collapse and (lambda s: "". Join (S.split ())) or (lambda s:s)
In Visual Basic, you are most likely to create a function that takes a string argument and a collapse parameter, and uses the if statement to determine whether to compress the white space before returning the corresponding value. This approach is inefficient because the function may need to handle every possible situation. Every time you call it, it will have to decide if you want to compress the blanks before giving them what you want. In Python, you can take the decision logic out of the function and define a cut-off Lambda function to provide the exact (unique) you want. This approach is more efficient, more elegant, and rarely leads to bugs that are annoying (oh, think those parameters are dizzy).
Using this example, we found that the use of lambda is a lot easier to simplify the code and make the code concise and clear. However, it is important to note that this reduces the readability of the code to some extent. People who are not very familiar with Python may find this incomprehensible.
Lambda defines an anonymous function
Lambda does not increase the efficiency of the program, only makes the code more concise.
If you can use for...in ... If to complete, be resolute without lambda.
If you do not include loops within LAMBDA,LAMBDA, if so, I would rather define functions to do so that the code is reusable and better readable.
Summary: Lambda exists to reduce the definition of a single-line function.
Python Lambda Introduction