1.LAMBDA-expression
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)
3.map () 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 topic.alibabacloud.com (www.php.cn)!