Python basics-8 lambda expressions, filter, and map built-in functions

Source: Internet
Author: User
Python basics-8 lambda expressions and filter, map built-in functions 1. Lambda expressions
Lambda Expression Syntax: lambda parameter: operation on the parameter
Lambda expressions are introduced mainly because of the following:
(1) when writing some execution scripts in Python, you can use Lanhu to save the process of defining functions.
(2) for some abstract functions that run the entire program, you only need to call them once or twice.
(3) simplify the readability of the code and do not need to adjust the definition of the function to read it.

Note that when using a Lambda expression, assign a value to a variable, and then use the variable like a call function to obtain the calculation result.

Def ds (x): return 2 * x + 1 print (ds (5) # The lambda expression is followed by a parameter, followed by a colon # The Operation lambda x: 2 * x + 1g = lambda x: 2 * x + 1 print (g (5 )) def add (x, y): return (x + y) print (add (3, 4) g = lambda x, y: x + yprint (g (3, 4 ))

The filter in the instance code is used to filter out 1 ~ An odd number between 10

List1 = list (filter (lambda x: x % 2, range (0, 10) print (list1)

The map () function calculates the iterated data one by one based on the ing defined by the first parameter.

List2 = list (map (lambda x: x * 2, range (10) print (list2)

The above are the expressions of lambda and built-in functions of filter and map in Python. For more information, see PHP Chinese website ( )!

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: 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.