What is the use of the map () method in python
Editor to share with you what is the use of the map () method in python, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Map () method:
Map () maps the specified sequence based on the function provided.
The first argument, function, calls the function function with each element in the argument sequence, returning a new list of values returned by each function function.
> map (lambda x: X * * 2, [1, 2, 3, 4, 5]) # provides two lists using lambda anonymous function [1, 4, 9, 16, 25] # to add list data in the same location > map (lambda x, y: X + y, [1, 3, 5, 7, 9], [2, 4, 6, 8, 10]) [3, 7, 11, 15, 19]
If the function has more than one parameter, but each parameter has a different number of sequence elements, it will be done according to the sequence of the least elements:
> listx = [1listz, 3Power3, 5pyror7] # 7 elements > listy = [2listz = [100100100100] # 4 elements > list_result = map (listy, listz) > > print (list (list_result)) [103,107,113,121]
You can obviously see that the z parameter in lambda actually uses listz, while there are only four elements in listz, so even if listx has seven elements and listy has six elements, it will not continue to execute, only the calculation of four elements will be performed.
Note:
Python 2.x returns the list.
Python 3.x returns an iterator object.
The above is all the content of the article "what is the use of the map () method in python?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!