Python lambda and pythonlambda Functions

Source: Internet
Author: User

Python lambda and pythonlambda Functions

Let's talk about python lambda functions.

 

Lambda is a keyword used in python to construct an anonymous function. It is called an anonymous function. An anonymous function is called a function that requires a function to complete the operation, but you are too lazy to name it, it happens that this function is not so commonly used, and it is estimated that it will not be used very much in the future. It will be used once, so the lambda function will be available soon.

 

Let's first look at a chestnut and add two numbers.

_ Author _ = 'mrchen' # function definition form def myAdd (x, y): 'add two digits 'Return x + yprint (myAdd (2, 3.4 )) # lambda usage form-1 myfunc = lambda x, y: x + yprint (myfunc (2, 3.4) # I don't want to name it anymore !!! For Mao, I need to define a temporary variable # Well. Let's free people who have a headache when talking about the function name # lambda usage form-2 print (lambda x, y: x + y) (2, 3.4 ))

Well, although the last function is unreadable, it actually reflects the value of lambda functions,

1. lambda functions can simplify the code by eliminating the hassle of defining functions.

2. You don't have to worry about the function name (if this function is commonly used, you still need to define the function)

3. Using lambda functions sometimes makes the code easier to understand

 

Let's see how lambda functions are used.

In a lambda function, after lambda, the part before the colon is the function parameter, which can be separated by commas (,). After the colon is the return value (it seems that lambda does not support multi-row operations, the return value is directly followed by the colon. It is a single row operation. If the function requires multiple rows, use def. In my opinion, Do not spray it)

 

For example:

Lambda x: x ** 2

X is a parameter, and x ** 2 is the return value, which indicates that the square of a number is implemented.


If you need to directly calculate the result of a value, you can do this:

(Lambda x: x ** 2) (3)

The first bracket contains the lambda function body, and the second bracket is the lambda function parameter,Note that the lambda functions and parameters must be enclosed in parentheses.




In Python, anonymous function lambda should be used.

Lambda functions are generally used in functional programming. C/C ++/Java and so on are usually learned in procedural programming, so they do not often come into contact with lambda functions. In fact, this product has already been used in C ++. If you are familiar with stl iterators, you will know functions such as foreach in it. You need to provide a function, this is a pain point for the ancient C/C ++ language. Generally, a function is written out of the main function and then passed in the function pointer, which looks very inintuitive. Boosts uses some special syntax techniques to implement C ++ lambda. For example, for such a list L, find the element set L = [1, 2, 3, 4, 5] where L is greater than 3 for procedural programming, normally, L3 = [] for I in L: if I 3: L3.append (I) is written in this way, you only need to provide the filter function with a judgment function. def greater_than_3 (x): return x 3L3 = filter (greater_than_3, L). This judgment function is very simple, writing with def is too cumbersome, so implementing it with lambda is very simple and easy to understand. L3 = filter (lambda x: x 3, L) is a very simple example, we can see the benefits of lambda. Lambda functions are more commonly used in map and reduce functions. Of course, lambda functions are not good either. They can also be used very complicated. For example, the answer to this question can be solved using a sentence like python, this lambda function looks really hard.

For lambda functions of python3

>>> Aa = map (lambda x: x ** 2, [1, 2, 3, 4, 5])
>>> Result = []
>>> For I in aa:
Result. append (I)

>>> Result
[1, 4, 9, 16, 25]
>>>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.