Python built-in map () and reduce () function.
If you read Google's famous essay papers "MapReduce: Simplified Data Processing on Large Clusters", you can probably understand the concept for map / reduce.
We look at the map. map () function accepts two arguments, a function, a sequence is, map the incoming function sequentially applied to each element of the sequence, and to return the result as a new list.
Illustration, we have such a function f (x) = x2, should this function on a list [1, 2, 3, 4, 5, 6, 7, 8, 9], the can use map () to achieve the following:
Now, we use the Python code to achieve:
1
2
3
4
5
|
>>>
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
]
|
map () is the first argument of f, i.e. the function object itself.
You might be thinking, you do not need to map () function, write a loop, you can calculate the results:
1
2
3
4
|
L
=
[]
for
n
in
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]:
L.append(f(n))
print
L
|
Indeed, however, from the top of the loop code, we can understand at a glance "the f (x) action and the results to generate a new list in each element list of" do?
Therefore, Map () function as a higher order, the fact that the operation rules abstract, and therefore, we can not only computationally simple f (x) = x2, also in arbitrary complex functions, such as digital-to-all this list a string:
1
2
|
>>>
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.
Look reduce the usage. reduce to a function on a sequence [x1, x2, x3 ...], this function must receive two parameters, and continue to reduce the result of the next element in the sequence do cumulative basis, the effect is:
reduce(f, [x1, x2, x3, x4]) = f(f(f(x1, x2), x3), x4)
Let's say a sequence of summation, it can be used reduce implementation:
1
2
3
4
5
|
>>>
def
add(x, y):
...
return
x
+
y
...
>>>
reduce
(add, [
1
,
3
,
5
,
7
,
9
])
25
|
Of course, the summation may be a direct function of the built-in Python sum (), is not necessary to use reduce.
But if we want the sequence [1, 3, 5, 7, 9] into integer 13579, reduce can come in handy:
1
2
3
4
5
|
>>>
def
fn(x, y):
...
return
x
*
10
+
y
...
>>>
reduce
(fn, [
1
,
3
,
5
,
7
,
9
])
13579
|
This example itself is not much use, however, considering the string str is a sequence, for example above a little change, with map (), we can write the str converted to int function:
1
2
3
4
5
6
7
8
|
>>>
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]
...
>>>
reduce
(fn,
map
(char2num,
'13579'
))
13579
|
Organized into a str2int function is:
1
2
3
4
5
6
|
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))
|
It can be further simplified using lambda functions to:
1
2
|
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]
|
1
2
|
def
str2int(s):
return
reduce
(
lambda
x,y: x
*
10
+
y,
map
(char2num, s))
|
That is, assuming Python does not provide int () function, you can write your own function to convert a string to an integer, and requires only a few lines of code!
Exercise
Use map () function, the user input of non-standard English name, becomes the first letter capitalized, other specifications lowercase names. Input: [ 'adam', 'LISA', 'barT'], output: [ 'Adam', 'Lisa', 'Bart'].
Python provides the sum () function can accept a list and sums, write a prod () function can accept a list and use reduce () quadrature.