Home  >  Article  >  Backend Development  >  Comprehensive analysis of Lambda expressions in Python

Comprehensive analysis of Lambda expressions in Python

WBOY
WBOYOriginal
2016-12-05 13:27:191473browse

What is a Lambda expression

"Lambda expression" (lambda expression) is an anonymous function. Lambda expression is named based on the lambda calculus in mathematics. It directly corresponds to the lambda abstraction (lambda abstraction). It is an anonymous function, that is, a function without a function name. . Lambda expressions can represent closures (note that they are different from the traditional mathematical sense).

Lambda is an anonymous function. When we need to call a certain function repeatedly and don’t want to write so much code, we can use lambda expressions instead.

Universal format of lambda:

lambda argument: manipulate(argument)

Sample code:

add = lambda x,y : x + y
print add(3,5)
#output: 8

Usage:

Sort.

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)]

The above is the Lambda expression in Python introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank you all for your support of the Script House website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn