There are three basic functions in Python, namely filter(), map() and reduce(). They provide us with filtering, mapping and aggregation functions respectively. In the previous section, we simply used the filter() function combined with an anonymous function. Below we will introduce its usage in detail.
When filtering and filtering data, we usually use the filter() function to help us solve the problem quickly. Its syntax format is:
filter(function, iterable object)
In the filter function, put our filtering or filtering method, that is, the function name , in the front, and store the iterable object in the back. Let's look at the following example:
deftest(x):ifx%2==0:returnxmy_list=[1,2,3,4,5,6]print(filter(test,my_list))#Just need some function names, no parameters are needed
Output result:
<filterobjectat0x034C2DD8>
Regarding this example, we first define a test() function, which returns its value if x is an even number, and then uses the test() function to filter the my_list list through the filter() function, but the output result is <filter object at 0x034C2DD8> , here we need to note that the return value of the filter() function is an iterable object, and we need to access the values iteratively, or use the list() function to force type conversion.
deftest(x):ifx%2==0:returnxmy_list=[1,2,3,4,5,6]print(filter(test,my_list))foriinfilter(test,my_list):print('after iteration Data:',i)print('Use list() method:',list(filter(test,my_list)))
The output is:
Data in the iteration: 2 Data in the iteration: 4 Data in the iteration: 6 Using the list() method: [2, 4, 6]
We have used the map() function many times before. For example, when we input multiple values, we will use the map() function. When we need to input four values:
a,b,c,d=map(int,input().split())print(a,b,c,d)
The syntax format of the map() function is:
map(function, iterable object)
When using the map() function, we mostly use it for data processing. The data in the iterable object is stored after being processed by the function. We continue to use the list() function for storage.
Let's first look at the above example of inputting four values. Int is a function, and the value input by input().splite is an iterable object, which is stored in the map object after being processed by the int function.
We can use the map() function to process all the data in a sequence through a function, see the following example:
We store the letters in a list, and if a lowercase letter exists, make it uppercase.
The code is as follows:
deftest(x):ifx.islower():returnx.upper()else:returnxmy_list=['d','o','t','C','p','P']print(list(map (test,my_list)))
The output is:
['D','O','T','C','P','P']
The test() function will first judge x. If it is a lowercase letter, it will return its uppercase letter. If it is not a lowercase letter, it will return its value.
The reduce() function is used to aggregate iterable objects through functional methods.
The syntax format is:
reduce(function, iterable object[, initial value])
For example, we know that a list is [1,2,3,4], and we need to find the sum of all the items in the list multiplied in sequence. We can use the reduce() function.
fromfunctoolsimportreduce#reduce function is defined in the functools module and needs to be introduced deftest(x,y):returnx*ymy_list=[1,2,3,4]print(reduce(test,my_list))
The output is:
The first line of code introduces this method, and the module will be explained later. The test() function returns two data to be multiplied, and then the my_list list is processed through the reduce() function.
The processing process is as follows:
Execute the first step first, then get a result and multiply it with the next one, and then go to the last digit.
The first two of these three functions are commonly used. Mastering these three functions can help us solve a series of complex problems. In the next section, we will learn about recursive functions.