This article begins with the use of Python's higher-order function map/reduce/filter, and more on: Python Learning Guide
Map/reduce
Python has built map() -in and reduce() functions.
If you read Google's famous paper "Mapreduce:simplified Data processing on Large Clusters", you can probably understand the concept of map/reduce.
Let's look at map first. The map() function receives two parameters, one is a function, the other is a sequence, the map incoming function functions sequentially to each element of the sequence, and returns the result as a new list.
For example, if we have a function f (x) = x two times, to function on a list [1,2,3,4,5,6,7,8,9] , it can be map() implemented as follows:
Now, we use Python code to implement:
def f(x): return* x>>>map(f, [1,2,3,4,5,6,7,8,9])[1,4,9,16,25,36,49,64,81]
map()The first nibble that is passed in is the f function object itself.
You might think that you don't need map() a function, write a loop, or you can calculate the result:
= []forin [1,2,3,4,5,6,7,8,9]: L.append(f(n))print L
Yes, but, from the loop code above, can you see "putting F (x) in every element of the list and generating a new list"?
So, map() as a high-order function, in fact it abstracts the arithmetic rules, so we can not only calculate the simple f (x) = x two times, but also can calculate any complex function, for example, the list of all the numbers into a string:
>>>map(str, [123456789])['1''2''3''4''5''6''7''8''9']
Only one line of code is required.
Let's look at the usage of reduce. Reduce functions a function in a sequence [X1, x2, x3 ...] , the function must receive two parameters, and reduce calculates the result and the next element of the sequence, and the effect is:
reduce= f(f(f(x1, x2), x3), x4)
For example, to sum a sequence, it can be implemented with reduce:
def add(x, y): return+ y>>>reduce(add, [13579])25
Of course, the sum operation can be built directly into Python sum() , and no need to use reduce.
But if you want to turn the sequence [1, 3, 5, 7, 9] into an integer 13579,reduce it will come in handy:
def fn(x, y): return*10+ y>>>reduce(fn, [13579])13579
This example is not very useful in itself, but if you consider that the string str is also a sequence, with a slight change to the above example, the matching map() function, we can write the str conversion to int the function:
deffn (x, y):returnX* Ten +YdefChar2num (s):return{' 0 ':0,' 1 ':1,' 2 ':2,' 3 ':3,' 4 ':4,' 5 ':5,' 6 ':6,' 7 ':7,' 8 ':8,' 9 ':9}[s]>>>Reduce(FN,Map(Char2num,' 13579 '))13579
str2intthe function that is organized into one is:
def Str2Int (s): def fn (x, y): return x * 10 + y def Char2num (s): return { ' 0 ' : 0 , ' 1 ' : 1 , ' 2 ' : 2 , ' 3 ' : 3 , ' 4 ' : 4 , ' 5 ' : 5 , ' 6 ' : 6 , ' 7 ' : 7 , ' 8 ' : 8 , ' 9 ' : 9 }[s] return reduce (FN, map (Char2num, s))
The
can also be further simplified with a lambda function:
def Char2num (s): return { ' 0 ' : 0 , ' 1 ' : Span class= "DV" >1 , ' 2 ' : 2 , ' 3 ' : Span class= "DV" >3 , ' 4 ' : 4 , ' 5 ' : Span class= "DV" >5 , ' 6 ' : 6 , ' 7 ' : Span class= "DV" >7 , ' 8 ' : 8 , ' 9 ' : Span class= "DV" >9 }[s]def Str2Int (s): return reduce (lambda x,y:x* 10 + y, map (Char2num, s))
That is, assuming that Python does not provide a int() function, you can write a function that converts the string to an integer by itself, and only requires a few lines of code!
Filter
Python's built-in filter() functions are used to filter the sequence.
and map() similar, filter() also receive a function and a sequence. And the map() difference is that the filter() incoming function functions sequentially with each element, and then depending on whether the return value is True or decides to False persist or discard the element.
For example, in a list, delete even numbers, keep only odd numbers, and you can write:
def is_odd(n): return%2==1filter(is_odd, [1245691015])# 结果: [1, 5, 9, 15]
To delete an empty string from a sequence, you can write:
def not_empty(s): returnand s.strip()filter(not_empty, ['A''''B'None'C'' '])# 结果: ['A', 'B', 'C']
filter()the key to using this higher-order function is to implement a "filter" function correctly.
Python high-order function _map/reduce/filter function