Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Summary of Python lambda expressions

Shulou Source: shulou.com Published: 2022-06-02 06:25:49 09月17日 Update

This article mainly explains the "Python lambda expression summary", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "Python lambda expression summary" bar!

Lambda expressions are anonymous functions, and they are also seen as anonymous functions without a specific name in other languages. Lambda expressions are very useful because they can make your code more concise. If you don't want to run a function twice in your program, you might want to use lambda expressions, which are exactly the same as ordinary functions. Let's take a look at the prototypes of lambda expressions, code examples, list sorting, list sorting, and lambda expression pros and cons.

1. Prototype:

In the lambda statement, the colon is preceded by a parameter, which can be separated by a comma, and the return value to the right of the colon. What the lambda statement builds is actually a function object. If there are no arguments, then the lambda colon does not precede it.

Lambda parameters: operations (parameters)

2. Code case:

Add = lambda x, y: X + y

Print (add (3,5))

# Output: 8

3. Sort the list

A = [(1,2), (4,1), (9,10), (13,-3)]

A.sort (key=lambda x: X [1])

Print (a)

# Output: [(13,-3), (4, 1), (1, 2), (9, 10)]

4. Sort the list and sort

Data = zip (list1, list2)

Data.sort ()

List1, list2 = map (lambda t: list (t), zip (* data))

5. The advantages and disadvantages of lambda expressions.

Functions in Python are more functional and adaptable than lambda expressions, so lambda expressions create simple function objects. On the other hand, the advantages of lambda expressions are also obvious. For example, for single-line code functions, using lambda expressions can save the complex process of defining functions and make the code more concise. For functions that do not need to be used many times, the performance of the program is improved because the lambda expression is released immediately after it is used up.

Thank you for your reading, the above is the content of "Python lambda expression summary", after the study of this article, I believe you have a deeper understanding of the problem of Python lambda expression summary, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Expressions functions code parameters sorting colons learning brevity pros and cons content prototypes objects cases programs statements complex obvious common advantages functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Redmi Microsoft vpn