Python lambda expression

Source: Internet
Author: User

Lambda is just an expression, the function body is much simpler than def, many times defining Def, and then writing a function is too cumbersome to define an anonymous function with lambda.

The body of a lambda is an expression, not a block of code. Only a finite amount of logic can be encapsulated in a lambda expression.

A lambda expression acts as a function sketch. Allows the definition of a function to be embedded within the code.

1 List (filter (lambdaifelse False, Range (100)))

As shown above, a lambda expression is used to define an anonymous function that filters multiples of 3 within 100 and generates a list.

def make_repeat (n):     return Lambda s:s * n

Of course, Lambda can also be nested within a function, as above, a lambda expression nested inside a function.

Double = Make_repeat (2) Double<function make_repeat.<locals>.<Lambda> at 0x0000000003a01d90>

Then, you can use a variable to receive it, display a double variable, a double variable is a function, and need a parameter, see lambda expression, need an S-parameter.

Print (Double (8))16

Finally, the double variable is called and passed to the parameter 8, resulting in a return value of 16. Because the previous value of N passed in is 2, 2 * 8 gets 16.

Built-in BIF Introduction:

Filter (): Simply understood as a filter, requires two parameters, function, and a sequence (strings, lists, tuples are sequences), the filter will sequentially pass the value of the sequence into the function,

Returns true if it is regenerated by a list.

1  List (filter (lambdaifelse False, range))2 [0, 3, 6, 9, 12, 15, 18, 21, 2 4, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 57, 60, 63, 66, 69, 72, 75, 78, 81, 84, 87, 90, 93, 96, 99]

Zip (): the literal meaning of understanding, that is, zip packaging, you can package multiple sequences, it will split the sequence, and then the first sequence and the second sequence of elements to form a tuple, 2 groups of groups to synthesize the list.

Note, however, that this is combined in the shortest sequence, meaning that if a sequence is longer and a shorter one, the combination will only go to the last element of the broken sequence, and the excess will be discarded.

1>>> str1 ="ABCDE"2>>> str2 ="ABCDEFG"3>>>list (Zip (str1, str2))4[('a','a'), ('b','b'), ('C','C'), ('D','D'), ('e','e')]

Map (): Mapping, usage and filter () are similar, but also the sequence is put into a function to operate, but regardless of the result of the operation, map () will be faithful feedback, which is the main difference between map () and filter (). Note that the function in filter () and map () must have a return value.

 1  >>> list (map (lambda  x: True if  x 3 = = 0 else  False, Range (100 2  [True, False, False, True, False, False, True, False, False, True, Fals  E, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, true, False, False, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, F Alse, True, False, False, True, False, False, True, False, False, True, False, False, True, False, Fal SE, True, False, False, True, False, False, True, False, False, True, False, False, True, False, False , True, False, False, True, False, False, True, False, False, True, False, False, True, False, False, true] 

Python lambda expression

Related Article

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.