About bloggers : Former Internet manufacturer tencent staff , Network security giant Venustech staff , Alibaba cloud development community expert blogger , WeChat official account java Quality creators of basic notes ,csdn High quality creative bloggers , Entrepreneur , Knowledge sharers , Welcome to your attention , give the thumbs-up , Collection .
In the actual development process , You will often encounter many identical or very similar operations , At this time , Code that implements similar operations can be encapsulated as functions , Then call the function where you need it . This can not only realize code reuse , It can also make the code more organized , Increase code reliability . Now let's introduce python Function of lambda Expression related content .
keyword lambda Used to define a special function —— Anonymous functions , also called lambda function . Anonymous functions are not nameless , Instead, the function name is returned as the result of the function , The syntax is as follows :
Function name = lambda [ parameter list ]: expression
In short ,lambda Functions are used to define simple 、 Functions that can be represented on one line , Returns a function type . for example :
sum = lambda arg1, arg2: arg1 + arg2 # Definition lambda function
print(' The sum is :', sum(10, 20)) # call sum function
give the result as follows .
lambda Functions are often used when a function similar to a function is temporarily required , But I don't want to define functions . for example , As a built-in function sorted() And list method sort() Of key Parameters .
example : Given multiple student information ( Including student ID 、 Name and score ), Output after sorting by grades .
stu = [
{
'num':'201801','name':'Wangwu','score':89},
{
'num':'201802','name':'Liujun','score':95},
{
'num':'201803','name':'Limeng','score':85}] # Define student information
stu.sort(key = lambda x:x['score']) # Sort by grade
for s in stu:
print(' Student number :',s['num'],' full name :',s['name'],' achievement :',s['score']) # Output list
give the result as follows .
1、 Liao Xuefeng's official website
2、python Official website
3、Python Programming case tutorial
The above is about Python Function of lambda Expression related knowledge , You can refer to it , If you think it's good , Welcome to thumb up 、 Collection 、 Looking at , Welcome to wechat search java Basic notes , Relevant knowledge will be continuously updated later , Make progress together .