The main function of the map () function is to reduce the code significantly when you perform the same operation on a batch element.
Detailed map function Parameters:
Map (param1,param2,***)
param1: function name, functions to be processed for bulk elements
PARAM2~PARAMX: An Iterative object, (list, meta-ancestor, iterator ...) In other words: can execute for the x in Param: The statement can be all)
When map has only two parameters:
For example:
1 def function (x): 2 ... return x*x34 >>> list1 = [x-ray]5 > >> Map (function,list1)6 [1, 4, 9]7
When the map parameter is multiple:
Note: The number of map parameters and the number of function parameters of the relationship: the number of map parameters is x, the number of function is x-1, otherwise it will be an error
For example (in case of error):
1>>>deffunction (x):2...returnx*x3 ... 4>>> List1 = [a]5>>> List2 = [4,5,6]6>>> LIST3 = [7,8,9]7>>>map (FUNCTION,LIST1,LIST2,LIST3)8 Traceback (most recent):9File"<stdin>", Line 1,inch<module>TenTypeerror:function () takes exactly 1 argument (3 given)
The correct usage should be:
1 def function (x, y, z): 2 ... return x+y+z34 >>> list1 = []5 > >> list2 = [4,5,6]6 >>> list3 = [7,8,9]7 >>> Map (FUNCTION,LIST1,LIST2,LIST3)8 [12, 15, 18]
Python Advanced Map () function