Home >Backend Development >Python Tutorial >Method example of map() function in python

Method example of map() function in python

黄舟
黄舟Original
2017-10-03 05:45:031723browse

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:

[3, 7, 11, 15]

Similarly, the idea of ​​map function processing can also be applied to cluster servers , that is, split a lot of data, and then put each piece of data into different computers for parallel processing, and they are all calculations of the same mapping relationship, and the number of data does not increase or decrease. Then these processed data are gathered together for the reduce process.


As for how to handle the reduce() function in python? You can learn from this article.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn