Python built map()
and reduce()
function.
If you read Google's famous essay papers " MapReduce: Simplified ON the Data Processing Large Clusters ", you can probably understand the concept for map / reduce.
We look at the map. map()
Function takes two parameters, a function, a is the sequence, map
the function is passed 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]
on, can be used map()
to achieve the following:
Now, we use the Python code to achieve:
>>> 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()
The first argument is f
that the function object itself.
You might be thinking, no map()
function, write a loop, you can calculate the results:
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()
as a higher-order functions, in fact it is the abstract operation rules, therefore, we can not only computationally simple f (x) = x2, also in arbitrary complex functions, for example, this list all numbers into a string :
>>> 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:
>>> def add(x, y): ... return x + y ... >>> reduce(add, [1, 3, 5, 7, 9]) 25
Of course, you can use Python's built-summation function directly sum()
, no need to use reduce.
But if we want the sequence [1, 3, 5, 7, 9]
into integer 13579, reduce can come in handy:
>>> 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
convert int
function:
>>> 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:
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:
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] def str2int(s): return reduce(lambda x,y: x*10+y, map(char2num, s))
That is, assuming Python does not provide int()
a function, you can write your own function to convert a string to an integer, and requires only a few lines of code!