map when senior python function built-in functions
The syntax is: the Map (function, Iterable, ...)
Parameters:
function - Function
iterable - one or more sequences
will function acting iterable sequence each element, and returns the result of calling
primarily for parallel operation, very efficient
1. an input parameter, a list of input
# 1 a parameter DEF map_func (X): RES = X ** 2 return RES A1 = Map (map_func, [l, 2,3]) # directly returns object, e.g. <Map Object AT 0x000001FEF3457438> Print (A1 ) Print (List (A1)) # cast: [1, 4, 9]
2. an input parameter, input tuple
a2 = map(map_func, (1,2,3)) print(a2)
3. The two input parameters
# 2. The two parameters DEF map_func_2 (X, Y): RES = X + Y return RES A3 = Map (map_func_2, [l, 2,3], [l, 2,3]) # simultaneously taken out from the two sequences elements of the same position for operation; however be given different lengths of the two parameters Print (A3) Print (List (A3)) # output is: [2, 4, 6]
4. int and other types of function
# 4. Typical applications: int type functions like A4 = Map (int, [1.5,2.1,3.0]) # removed simultaneously from the two elements of the same position in the sequence, calculates; however, the length will be given two different parameters print (List (A4)) # output: [1, 2, 3] A5 = Map (int, ' 12306 ' ) # string element becomes an integer Print (List (A5)) # output: [1, 2, 3 , 0, 6]
5. Using a lambda expression
# 5. lambda functions, x is the parameter, x ** 2 belonging to the return value of the lambda expression A6 = Map ( lambda X: X ** 2, [. 1, 2,. 3 ]) Print (A6) Print (List ( A6)) # output: [1, 4, 9]
reduce function, and map functions similar note in Python3 the reduce is no longer a built-in function, but integrated into the functools, you need: from Import reduce functools
Function to a data set ( list, tuple all data, etc.) in the following: a function function (passed to reduce the binary function, two parameters , the first of the set of elements 1, 2) operation, the resulting data then the third function operation function , proceed sequentially until the last.
用法:reduce(function, iterable[, initializer])
Parameters:
function - a function, there are two parameters
iterable - iterables
initializer - Optionally, the initial parameter
1. "Reduction" is a value
from functools Import the reduce DEF add_2 (X, Y): return X + Y A7 = the reduce (add_2, [l, 2,3]) # obtained value is a sequentially executed add_2 (1,2), and the results of execution 3 add_2 (add_2 (1,2), 3 ), somewhat similar to the recursive computation Print (A7) # outputs: 6
2. lambda expression calls
# Lambda function used, x, y is the parameter, x + y is the return value of the lambda expression A8 = the reduce ( lambda X, Y: X + Y, [l, 2,3]) # obtained value is a first call parameters 2, 3, and then continue to use the results Print (A8) # outputs: 6
3. Reduce the matrix dimensions
# Reduced matrix dimensions Import numpy AS NP tmp = np.mat ([[l, 2,3], [4,5,6], [7,8,9 ]]) Print (tmp) # outputs: # [ [2. 3. 1] # [. 4. 5. 6] # [. 7. 8. 9]] A9 = the reduce ( the lambda X, Y: X + Y, tmp) # actual: first [2,3] + [4,5 , 6] = [5,7,9]; and [5,7,9] + [7,8,9] = [12,15,18] Print (A9) # output: [[121518]] , a (1,3) matrix
Among them, there are filter functions are also similar.
reference:
https://baijiahao.baidu.com/s?id=1594702528079035916&wfr=spider&for=pc
https://blog.csdn.net/wxjsjp/article/details/80638696
https://www.runoob.com/python/python-func-map.html