First, Map
Python built-in functions, usage and description are as follows:
Class Map (object): "" " Map (func, *iterables)---map object make a iterator that computes the function usi ng arguments from each of the iterables. Stops when the shortest iterable is exhausted. "" "
The map () function receives two parameters, one is the function, the other is to function the Iterable
map
incoming function to each element of the sequence sequentially, and returns the result as a new one Iterator
.
For example, we have a function f (x) =x2, to function on a list [1, 2, 3, 4, 5, 6, 7, 8, 9]
, it can be map()
implemented as follows:
def f (x): return x * XR = map (f, [1, 2, 3, 4, 5, 6, 7, 8, 9]) list (r) [1, 4, 9, 16, 25, 36, 49, 64, 81]
The first parameter that the map () passes in is the f
function object itself. Since the result r
is one Iterator
, it is an Iterator
inert sequence, so the list()
function allows it to calculate the entire sequence and return a list.
#使用lambda匿名函数list (Map (Lambda x:x * x, [1, 2, 3, 4, 5, 6, 7, 8, 9])) [1, 4, 9, 16, 25, 36 , 49, 64, 81]
map () is a high-order function, in fact it abstracts the arithmetic rules, so we can not only calculate the simple f (x) =x2, but also can calculate any complex function, for example, the list all the 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 ']
Advantages of the Map function:
- The function logic is clearer, and the parameter ' F ' indicates the operation of the element.
- Map is a higher-order function that can perform more abstract operations
Second, reduce
def reduce (function, sequence, initial=none): # Real signature unknown; Restored from __doc__ "" " reduce (function, sequence[, initial]), value Apply a function of of the arguments Cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value. For example, reduce (lambda x, Y:x+y, [1, 2, 3, 4, 5]) calculates ((((( 1+2) +3) +4) +5). If Initial is present, it's placed before the items of the sequence in the calculation, and serves as a default when The sequence is empty. "" Pass
reduce takes a function on a sequence [x1, x2, x3, ...]
, the function must receive two parameters, the reduce
result continues and the next element of the sequence is accumulated, the effect is:
Reduce (f, [X1, x2, X3, x4]) = f (f (f (x1, x2), x3), x4)
For example, to sum a sequence, it can be reduce
implemented by:
From Functools import reducedef Add (x, y): return x + yreduce (add, [1, 3, 5, 7, 9]) 25
anonymous function implementations:
Reduce (lambda x, y:x + y, [1, 3, 5, 7, 9]) 25
Of course, the sum operation can be directly built into Python functions sum()
, no need to use reduce
.
But if you want to [1, 3, 5, 7, 9]
transform the sequence into integers 13579
, you can put reduce
it in handy:
From Functools import reducedef fn (x, y): return x * + yreduce (FN, [1, 3, 5, 7, 9]) 13579
anonymous function implementations:
Reduce (lambda x, y:x * + y, [1, 3, 5, 7, 9]) 13579
This example is not very useful in itself, but if we consider that the string str
is also a sequence, with a slight change to the above example, map()
we can write the str
converted int
function:
From Functools import reducedef fn (x, y): return x * + ydef 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
str2int
the function that is organized into one is:
From Functools import reducedef str2int (s): 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 ': 8, ' 9 ': 9}[s] return reduce (FN, map (Char2num, s))
You can also use lambda functions to further simplify:
From Functools import reducedef char2num (s): return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 8 ': 8, ' 9 ': 9}[s]def str2int (s): return reduce (lambda x, y:x * + y, map (Char2num, s))
Small exercise:
- The use
map()
of functions, the user entered the non-standard English name, the first letter capitalized, other lowercase canonical name. Input: [‘adam‘, ‘LISA‘, ‘barT‘]
, output: [‘Adam‘, ‘Lisa‘, ‘Bart‘]
:
List (Map (lambda x:x.capitalize (), [' Adam ', ' Lisa ', ' Bart ')]) [' Adam ', ' Lisa ', ' Bart ']
2. The function provided by Python sum()
can accept a list and sum, write a prod()
function that accepts a list and takes advantage of the quadrature reduce()
:
def prod (l): return reduce (lambda x, y:x * y, l) L = [1, 2, 3, 4, 5]print (PROD (l)) 120
anonymous function implementations:
Reduce (lambda x, y:x * y, [1, 2, 3, 4, 5]) 120
3. Use map
and reduce
write a str2float
function to convert a string ‘123.456‘
into a floating-point number 123.456
:
From Functools import reducedef char2num (s): return {' 0 ': 0, ' 1 ': 1, ' 2 ': 2, ' 3 ': 3, ' 4 ': 4, ' 5 ': 5, ' 6 ': 6, ' 8 ': 8, ' 9 ': 9}[s]def str_split (s): s1, s2 = s.split ('. ') return S1, S2def str2int_1 (S1): return reduce (lambda x, y:x * + y, map (char2num, S1)) def str2int_2 (S2): return (Reduce (lambda x, y:x * + y, map (char2num, S2)))/pow (Ten, Len (S2)) def str2float (s): s1, s2 = str_split (s) Res = Str2int_1 (S1) + str2int_2 (S2) return resa = str2float (' 123.456 ') print (a) 123.456
Pending Update:
Python Foundation-map/reduce/filter