lambda expression in Python

Source: Internet
Author: User

The lambda expression in C + + C++11 joins the standard library, which is a short, anonymous callable object that the compiler translates into an anonymous class object. The most important feature of lambda expressions is that they are short and flexible and easy to invoke. It does not need to handle very complex logic, usually containing only one or two short lines of code.

Python, as an elegant and concise scripting language, naturally does not miss this mechanism. The lambda expression in Python is in the following form:

Lambda parameter list: expression

If there are no arguments to pass, the parameter class table can be empty, but the expression part cannot be empty because the lambda expression must have a return value.

A lambda expression returns a value that can be a result of a calculation, or it can be a new lambda expression

Returns a value:

>>> f = lambda x,y:x+y//Create a lambda expression >>> print f (1, 2)        //Call lambda expression 3
returns a new lambda expression:

>>> f = lambda x:lambda y:x+y  //nested lambda expression >>> a = f (1)                       //f (1) Returns a lambda expression >>> b = A (ten) >>> print B11


Here's a look at the three Python built-in functions that are often used with lambda expressions: filter,map,reduce

1.Filter (BOOL_FUNC,SEQ): iterates through each element in the list seq, passing in a function Bool_func with the return value of type bool, filtering out the list of elements with a function return value of true, and getting a new list

<span style= "font-size:10px;" >>>> filter (lambda x:x > 3, [1, 2, 3, 4, 5]) [4, 5]</span>
2.map (func,seq1[,seq2 ...]) : Maps a list to a new list through the Func function
>>> map (Lambda x:x*2, [1, 2, [3, 4]]) [2, 4, [3, 4, 3, 4]]

3. Reduce (Func,seq[,init]): Here the Func function accepts two parameters, and reduce sequentially takes one element from the SEQ into the Func function, and the other parameter is the return value of the last Func function. The third parameter is an optional parameter, and if INIT is specified, the first element in Init and SEQ is initially passed to Func, and if not specified, the initial two elements of the SEQ are taken to Func.

<span style= "font-size:10px;" >>>> Reduce (lambda x,y:x + y,[1,2,3,4])  >>> reduce (lambda x,y:x + y,[1,2,3,4],10)  </span>




Lambda expression in Python

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.