The map () and reduce () functions are built in Python.
If you've 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 the map first. The map () function receives two parameters, one is a function, the other is a sequence, and map functions the passed-in function to each element of the sequence and returns the result as a new list.
For example, for example, we have a function f (x) =x2, to use this function in a list [1, 2, 3, 4, 5, 6, 7, 8, 9], you can do this with a map ():
Now we're going to use Python code:
>>> def f (x):
... return x * x ...
>>> map (f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
[1, 4, 9, 16, 25, 36, 49, 64, 81]
The first parameter passed by map () is F, which is the function object itself.
You might think that you don't need a map () function, write a loop, or you can calculate the result:
L = [] for
n in [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 "f (X") acting on every element of the list and generating a new list of the results?
So, as a higher order function, map () actually abstracts the rules of operation, so we can compute not only the simple f (x) =x2, but also any complex function, for example, to convert all numbers of this list into strings:
>>> map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9]) [' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 '
]
Only one line of code is required.
See the use of reduce again. Reduce functions a function in a sequence [X1, x2, x3 ...] , this function must receive two parameters, reduce to continue the result and the next element of the sequence cumulative calculation, the effect is:
Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)
For example, to sum a sequence, you can use reduce to implement:
>>> def add (x, y):
... return x + y
...
>>> reduce (add, [1, 3, 5, 7, 9])
25
Of course the sum can be used directly with the Python built-in function sum (), no need to use reduce.
But converting a sequence [1, 3, 5, 7, 9] into an integer 13579,reduce can be useful:
>>> def fn (x, y):
... return x * + y
...
>>> reduce (FN, [1, 3, 5, 7, 9])
13579
The example itself is of little use, but if you take into account that string Str is also a sequence, with a slight change to the example above, with map (), we can write a function that converts str to int:
>>> def fn (x, y):
... return x * + y
...
>>> def char2num (s):
... return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]
...
>>> reduce (FN, map (char2num, ' 13579 '))
13579
The function of organizing into a str2int is:
def str2int (s):
def fn (x, y): return
x * ten + 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))
You can also use a lambda function to further simplify:
def char2num (s): return
{' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 7 ': 7, ' 8 ': 9}[s]
def str2int (s): return
reduce (lambda x,y:x*10+y, map (Char2num, s))
In other words, assuming that Python does not provide an int () function, you can write a function that converts a string to an integer, and it only takes a few lines of code!
Practice
Using the map () function, the user entered the nonstandard English name into the first letter uppercase, other lowercase canonical names. Input: [' Adam ', ' Lisa ', ' Bart '], output: [' Adam ', ' Lisa ', ' Bart '].
The sum () function provided by Python can accept a list and sum, write a prod () function that accepts a list and uses reduce () to accumulate.