Python's lambda function---notes

Source: Internet
Author: User

Guide to the development of PYTHON3 programs

The lambda function, which is an anonymous function , creates the syntax:

Lambda parameters:express

Parameters: Optional, if provided, is usually a comma-delimited form of variable expressions, that is, positional parameters.

Expression: You cannot include a branch or loop (but allow a conditional expression ), and you cannot include a return (or yield) function. If it is a tuple , the parentheses are applied to enclose it.

Call the lambda function, and the result returned is the result of the evaluation of the expression .

Determines whether S is yes or no>>> if the parameter is 1 = lambda x: "Yes" if x==1 else "no" >>> s (0) ' No ' >>> s (1) ' Yes '

In the example above, the If...else statement is reduced to a single conditional expression with the following syntax:

expression1 If A else expression2

If A is true, the result of the conditional expression is expression1, otherwise expression2

The following examples illustrate the use of lambda functions

 Use the sorted () method and the List.sort () method to sort elements=[(2,12, "A"), (1,11, "N"), (1,3, "L") , (2,4, "B")]>>> sorted (elements) [(1, 3, ' L '), (1, one, ' N '), (2, 4, ' B '), (2,, ' A ')], based on the elements of each tuple after the two items are sorted, E represents the list The parentheses of a lambda expression are required for each ternary element when the expression is a tuple and lambda is a parameter of a function >>> Elements.sort (key=lambda e: (e[1],e[2)) >> > elements[(1, 3, ' L '), (2, 4, ' B '), (1, one, ' N '), (2, B, ' A ')] shard way Get the same effect >>> Elements.sort (Key=lambda e:e[1: 3]) >>> elements[(1, 3, ' L '), (2, 4, ' B '), (1, one, ' N '), (2, B, ' A ')]>>> elements.sort (Key=lambda e: (e[2 ].lower (), e[1])) The following two methods are equivalent, all can be called by area (5,3), the result is the same >>> Area=lambda b,h:0.5*b*h>>> def area (b,h): Return 0.5*b*h default dictionary use, when accessing a key that is not present, create a new key, which is the non-existent key, the value of the lambda function value >>> import collections>>> one_dict = Collections.defaultdict (lambda:-1) >>> two_dict = Collections.defaultdict (lambda: (0,0)) >>> Three_ Dict = collections.defaultdict (lambda: "No message avaliable") 

  

Assertion???

Python's lambda function---notes

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.