A higher-order function satisfies any of the following two points:
1. function receives one or more functions as parameters
2. function returns a function
1 description
Calculates a new iteration object using functions and each element in an iterative object as a parameter
map () maps the specified sequence according to the provided function.
The first parameter function calls a function function with each element in the parameter sequence, returning a new list containing the return value of each function.
2 syntax
map (function, sequence[, sequence, ...])
Function: Functions
Sequence: one or more sequences
return value : New Iteration Object
3 Example 3.1 when an iteration parameter
The following code demonstrates a re-python3 of the conditions, and then python2 the operation of the map function slightly different, do not add print([ i for i in Y]) , can be directly output print (y),
defFun (x):returnX + 3y= Map (fun,[0,1,2]) Z= Map (Fun,range (3))Print('Fun :', fun)#Fun : <function in 0x7fea226eaf28>Print('y:', y)#y: <map object at 0x7fea20f59518>Print('Z:', z)#Z: <map object at 0x7fea20f594e0>Print(I forIinchY#<generator Object <genexpr> at 0x7fea22621f68>Print(I forIinchZ#<generator Object <genexpr> at 0x7fea22621f68>Print([I forIinchY])#[3, 4, 5]Print([I forIinchZ])#[3, 4, 5]
Run
Fun: <function in 0x7fea226eaf28><map object at 0x7fea20f59518><map object at 0x7fea20f594e0><generator object <genexpr> at 0x7fea22621f68><generator object <genexpr> at 0x7fea22621f68>[3, 4, 5] [3, 4, 5]
3.2 Two iteration parameters (elements within an iteration parameter are equal)
def Fun (x, y ): return x += map (fun,[0,1,2],[10,11,12= Map (Fun,range (3), range (10,13))Print for in Y]) #[Ten, A,]printfor in z]) #[Ten, A.]
Run
[10, 12, 14]
3.3 Two iteration parameters (elements are not equal within an iteration parameter)
When the elements in the iteration parameters are unequal, the minimum number of elements is the dominant, or the "Cask effect "
def Fun (x, y ): return x += map (fun,[0,1,2,3,4,5,6,7,8,9],[10,11,12= Map (Fun,range (3), range (10,20)) print for in Y]) #[Ten, A,]printfor in z]) #[Ten, A.]
The display problem of map function under 4 Python3
The map function is a more important function in Python, and the design is inspired by functional programming. This is how the map function is interpreted in the official Python documentation:
- Map (
function,
iterable,
... ) )
-
Return an iterator this applies function to every item of iterable, yielding the results. If additional iterable arguments is passed, function must take that many arguments and is applied t o the items from any iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.
That is, the first parameter received by the map function is a function that can be used for system functions such as a float, or a function defined by Def, or a function defined by a lambda.
To give a simple example, the following example can be displayed normally under Python2.7:
ls = [a]rs=map (str, LS)#Print Results['1','2','3']lt= [1, 2, 3, 4, 5, 6]defAdd (num):returnnum + 1RS=map (Add, lt)PrintRS#[2,3,4,5,6,7]
But under the Python3 we enter:
ls=[1,2,3]rs=map (str,ls)print(RS)
The display is:
<map at 0x3fed1d0>
Rather than the results we want, this is also some of the new changes that have taken place under Python3, if we want the desired results to be written like this:
ls=[1,2,3]rs=map (str,ls)print(List (RS)
This shows the result that we want to see. This is a little bit of help in the 10th chapter of machine Learning combat.
Transferred from: https://www.cnblogs.com/itdyb/p/5731804.html
Python Learning notes-higher-order function map ()