In python reduce / map / filter The three functions are easy to confuse. Although using functions to operate on elements in iterators or sequences, the applicable scenarios are different;
1, map function
Features of map function: perform the same operation on each element in the iterator or sequence (for example, + 1 for each element, etc.), and return the iterator or list. The example is as follows:
# !usr/bin/env python # -*- coding:utf-8 _*- """ @Author:Why worry? @Blog(Personal blog address): shuopython.com @WeChat Official Account(WeChat official account): Ape theory python @Github:www.github.com @File:python_reduce_list_map.py @Time:2020/3/6 10:25 @Motto:Not a small step to a thousand miles, not a small stream to become a river and sea, the wonderful program of life needs to be accumulated unremittingly! """ def func1(x): # Square each element print("x=%d x*x=%d"%(x,x*x)) return x*x if __name__ == "__main__": list1 = [1,2,3,4,5] #Method 1: value = map(func1,list1) #Returns the map object, which can be forced to list print(list(value)) print("***"*20) #Method two: value = map(lambda x:x*x, list1) #Returns the map object, which can be forced to list print(list(value))
Output results:
x=1 x*x=1 x=2 x*x=4 x=3 x*x=9 x=4 x*x=16 x=5 x*x=25 [1, 4, 9, 16, 25] ************************************************************ [1, 4, 9, 16, 25]
It is worth noting that the return value of the map function is an iterator. Note that the returned result can only be iterated once. If it needs to be used multiple times, please save and process the result in advance, for example:
def func1(x): # Square each element # print("x=%d x*x=%d"%(x,x*x)) return x*x if __name__ == "__main__": list1 = [1,2,3,4,5] value = map(func1,list1) #Returns the map object, which can be forced to list print(list(value)) print(list(value))
Output results:
[1, 4, 9, 16, 25] []
It's just silly, isn't it? Obviously, there is no mistake. Why is the second output an empty list? Because the iterator returned by the map function can only be iterated once, the solution is: when getting the result, the iterator is forced to List list The example is as follows:
def func1(x): # Square each element # print("x=%d x*x=%d"%(x,x*x)) return x*x if __name__ == "__main__": list1 = [1,2,3,4,5] value = list(map(func1,list1)) #Returns the map object, which can be forced to list print(list(value)) print(list(value))
Output:
[1, 4, 9, 16, 25] [1, 4, 9, 16, 25]
2, reduce function
Features of reduce function: apply a function with two parameters cumulatively to the items of a sequence from left to right, so as to merge the sequence into a single value (such as accumulation or cumulative multiplication list elements, etc.), and return the final calculation result, which is a value. The example is as follows:
#Python 3 needs to import modules when using the reduce function from functools import reduce # Import module def func1(x,y): # Take the result of the last calculation as the input of the next calculation print("x=%d y=%d x*y=%d"%(x,y,x*y)) return x*y if __name__ == "__main__": list1 = [1,2,3,4,5] #Method 1: value = reduce(func1,list1) #equivalence 1*2*3*4*5 = 120 print(value) print(type(value)) print("***"*20) #Method two: value = reduce(lambda x,y:x*y, list1) # equivalence 1*2*3*4*5 = 120 print(value) print(type(value))
Output results:
x=1 y=2 x*y=2 x=2 y=3 x*y=6 x=6 y=4 x*y=24 x=24 y=5 x*y=120 120 <class 'int'> ************************************************************ 120 <class 'int'>
3, filter function
Features of filter function: filter elements in iteratable objects according to specific conditions (such as filtering all even numbers in the list, etc.), as follows:
lis=[0,1,2,3,4,5,6] #Define common functions for filtering even numbers def func4(x): return x%2==0 #First use filter How to function---lambda res5=filter(lambda x:x%2==0,lis) print(list(res5)) print(list(res5)) print("***"*20) #Second use filter How to function---Ordinary function two res7=filter(func4,lis) print(list(res7)) print(list(res7))
Output results:
[0, 2, 4, 6] [] ************************************************************ [0, 2, 4, 6] []
Stupid? It has been proved that the result returned by the filter function is the same as that of the map function, which can only be iterated once. The solution is the same as that of the map solution, and the result is strongly converted to List list Yes.
Guess you like:
4. Difference between Python Gil Lock and mutex Lock
Please indicate: Ape says Python »python reduce/map/filter function differences