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 Iterable
, map
to pass successively to the function of each element of the sequence, and the result as a new Iterator
return.
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:
f(x) = x * x
│
│
┌───┬───┬───┬───┼───┬───┬───┬───┐
│ │ │ │ │ │ │ │ │
▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
[ 1 2 3 4 5 6 7 8 9 ]
│ │ │ │ │ │ │ │ │
│ │ │ │ │ │ │ │ │
▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼ ▼
[ 1 4 9 16 25 36 49 64 81 ]
Now, we use the Python code to achieve:
>>> def f(x): ... return x * x ... >>> r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> list(r) [1, 4, 9, 16, 25, 36, 49, 64, 81]
map()
The first argument is f
that the function object itself. Since the result r
is a Iterator
, Iterator
is an inert sequence, so by list()
the function it calculated the entire sequences and returns a list.
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 :
>>> list(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
usage. reduce
A role in the function of a sequence [x1, x2, x3, ...]
, the function must receive two parameters, reduce
the results and continues to 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
to achieve:
>>> from functools import reduce
>>> 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 an integer 13579
, reduce
you can come in handy:
>>> from functools import reduce
>>> 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:
>>> from functools import reduce
>>> def fn(x, y): ... return x * 10 + y ... >>> def char2num(s): ... digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} ... return digits[s] ... >>> reduce(fn, map(char2num, '13579')) 13579
Organized into a str2int
function is:
from functools import reduce
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return DIGITS[s] return reduce(fn, map(char2num, s))
It can be further simplified using lambda functions to:
from functools import reduce
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def char2num(s): return DIGITS[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!
Original URL: https: //www.liaoxuefeng.com/wiki/1016959663602400/1017329367486080