Summarize the basic knowledge lambda functions Python, map function, filter () function. Twelfth day one (novice can supervise each other)

Today, Sunday, the day in learning, updating some notes in the evening, hoping to better understanding of everyone, learning python ~

lambda functions, that is, we say that the anonymous function. It does not specify the name, function can also be called a word, I do not think too much, we look at the code to understand the next sentence function (lambda).

Its format is:

Lambda keyword followed by parameters, may be one or more. Immediately colon, and then followed by the expression.
. 1 C =. 5
 2 A = lambda C, A = 12 is: A + C    # Format: lambda keyword followed by parameters, it may be one or more. Immediately colon, and then followed by the expression. 
3  Print (A (c))                # call lamabda anonymous function, and then pass the variable c

How, throughout the lambda anonymous function, the function definition, then the parameter expression, is done in one line of code thing, is not so easy sometimes to more than def defined functions.

1 c = 5
2 def a(c,a=12):
3     a += c
4     print(a)
5 a(c)

They can compare def and lambda two forms, sometimes simple expressions, can use an anonymous function lambda functions, you can continue to use the defined function oh def function is more complex in time ~~

  Next we look at using the map function, man function has two parameters, the first parameter is: a function of the second parameter is: the data object can be iterative. map can be iterative data object elements, each element, while doing the same thing, we look at the code:

. 1 A = [1,2,3,4 ]
 2  DEF the Add (A):
 . 3      return A + 100     # so that each element of the list 100 is added, and returns the new list 
. 4  
. 5  Print (List (Map (the Add, a)))   # Note: map function, Python 3. several versions of the iterator returned, it is necessary () function preceded list conversion

Highlights:

1, map () parameters, the function may be a custom function, it may be a built-in function.

2, the second function needs to be iterative data, such as lists, tuples, dictionaries.

3, action map () function is a function of other parameters, so that the iteration parameters, each data element in the other functions performed again.

4, map function in the Python version 3. few Returns an iterator, so it is necessary () function to convert preceded list, if you are the other type, you make the appropriate conversion.

1 print(list(map(lambda a:a+100,a)))

Above a small case, we can write together with the map function and lambda functions. Is not it easier to understand?

 最后再认识一个:filter() 函数,它可以用于过滤序列,过滤掉不符合条件的元素,把符号条件的元素组合新的列表返回给你。filter()函数也有两个参数,第一个参数:是函数,第二个参数:是序列;将序列中的每个元素作为参数传入给函数进行过滤判断。将返回True的元素值放到新的列表中。

1 #过滤出列表中大于3的元素
2 a = [1,2,3,4,5]
3 
4 def add(a):
5     return a>3    #将每个元素进行过滤判断
6 print(list(filter(add,a)))    #将序列中的每个元素作为参数传入给函数进行过滤判断。将返回True的元素值放到新的列表中。

同样我们也可以结合lambda()函数:

filter()函数返回的也是迭代器,所以需要在前面加list()函数进行转换,如果你是其他类型,就进行相应的转换。

1 a = [1, 2, 3, 4, 5]
2 print(list(filter(lambda b:b>3,a)))

 

 hhh。。。。。。。周末要过去了,洗澡睡觉晚安~~

不懂的朋友可以多敲几遍代码,或者留言评论哦~~~~

 

Guess you like

Origin www.cnblogs.com/woshidaliua/p/11299876.html