Python 0 Basic Primer Eight lambda expressions and filter, map built-in functions

Source: Internet
Author: User
The lambda expression syntax is: Lambda parameter: An operation on a parameter
The introduction of lambda expressions is mainly due to the following points:
(1) When Python writes some execution scripts, it can save the process of defining a function using LANBDA.
(2) for some of the more abstract and the execution of the entire program only need to call one or two times the function, do not need to name the problem headache.
(3) Simplify the readability of the code, do not need to be transferred to the definition of the function to read.

Note that when you use a lambda expression, you assign a lambda expression to a variable, and then use that variable to get the result of the operation, just like the calling function.

def DS (x):     return 2*x+1print (DS (5)) #lambda表达式后面紧跟着的是参数, followed by a colon # followed by the operation on the parameter lambda X:2*x+1g=lambda x:2*x+1print ( G (5)) def add (x, y):     return (x+y) print (Add (3,4)) G=lambda x,y:x+yprint (g (3,4))

2.filter () function
Syntax: Filter (None or function,iterable)
The function is to select the data with the result of 1 or true in the iterated data, which is more convenient to use with lambda.
The role of filters in instance code is to filter out the odd numbers between 1~10

List1=list (Filter (lambda x:x%2,range (0,10))) print (List1) () function
The map () function is the calculation of the data that can be iterated by the mappings defined by the first parameter.

List2=list (Map (lambda x:x*2,range)) print (LIST2)

The basics of the function are over, and the next blog is about knowledge of the data structure of the dictionary.

The above is the Python 0 Basic primer Eight lambda expression and filter, map built-in function content, more relevant content please focus on (!

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