Home >Backend Development >Python Tutorial >Method example of map() function in python
map() is Python's built-in higher-order function. It receives a function f and a list, and by applying function f to each element of the list in turn, it obtains a new list and returns it. The following article mainly introduces how to use the map() function in python. Friends who need it can refer to it
Preface
There is a function map() in python, which feels a bit high-end. This article will give you a detailed introduction to the use of the map() function in python, and share it for your reference and study. I won’t say much below, let’s take a look at the detailed introduction:
Maybe you I have read GOOGLE's most profitable paper:
"MapReduce: Simplified Data Processing on Large Clusters"
Google's MapReduce paper said: Our abstraction is inspired by the map and reduce primitives present in Lisp and many other functional languages.
This sentence mentions the origin of the MapReduce idea. The general meaning is that MapReduce is inspired by the built-in functions map and reduce in functional languages (such as Lisp).
So what exactly does map() do?
In fact, the map() function is a mapping relationship from one data set to another data set. There is no reduction or addition of elements in the middle. Therefore, in python, the map() function takes out the elements from multiple list objects in order, then puts them into the function for operation and calculates the result. It is a parallel relationship and does not reduce elements.
For example:
##
#python 3. 6 #蔡军生 #http://blog.csdn.net/caimouse/article/details/51749579 # def sum(x, y): return x + y list1 = [1, 3, 5, 7] list2 = [2, 4, 6, 8] result = map(sum, list1, list2) print([x for x in result])
The output result is as follows:
Summarize
The above is the detailed content of Method example of map() function in python. For more information, please follow other related articles on the PHP Chinese website!