Lambda expression of Python learning

Source: Internet
Author: User

A lambda expression is an anonymous function, a function that does not have a function name. A lambda expression can represent closures (notice differs from the traditional mathematical sense).

1. Lambda expression in Python

A lambda expression is a special type of defined function in Python that allows you to define an anonymous function. Unlike other languages, the function body of a Python lambda expression can have only one statement, which is the return value expression statement. Its syntax is as follows:

Lambda parameter list: function return value expression statement The following is an example of a lambda expression:
1234 #!/usr/bin/envpythonli=[{"age":20,"name":"def"},{"age":25,"name":"abc"},{"age":10,"name":"ghi"}]li=sorted(li,key=lambdax:x["age"])print(li)
If you don't use a lambda expression and write a regular function, you need to do this:
123456 #!/usr/bin/envpythondefcomp(x):    returnx["age"]li=[{"age":20,"name":"def"},{"age":25,"name":"abc"},{"age":10,"name":"ghi"}]li=sorted(li,key=comp)print(li)

2. There are lambda expressions in C #, Java, C + +

Lambda expression of Python learning

Related Article

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.