Example of a method for the map () function in Python

Source: Internet
Author: User
Map () is a Python built-in high-order function that receives a function f and a list, and then, by putting the function f on each element of the list in turn, gets a new list and returns. The following article mainly introduces the use of the map () function in Python, the need for friends can refer to the following

Objective

In Python there is a function map (), which is a bit tall on the feel. This article will give you a detailed introduction to the Python map () function used in the relevant content, share it for everyone to reference the study, the following words do not say, come together to see the detailed introduction:

Maybe you've seen Google's most lucrative paper:

"Mapreduce:simplified Data processing on Large Clusters"

Google's MapReduce paper says: Our abstraction are inspired by the map and reduce primitives present in Lisp and many other functio NAL languages.

This phrase refers to the origins of the MapReduce idea, which roughly means that MapReduce is inspired by the built-in function map and reduce in functional languages such as Lisp.

So what exactly does map () do?

In fact, the map () function is the mapping relationship of a dataset to another dataset, and the middle does not decrease or add elements. So in Python, the map () function simply takes the elements of multiple list objects out of order, and then puts them into the function to manipulate them and calculate the results. It is a parallel relationship and does not reduce the element.

As in the following 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 results are as follows:

[3, 7, 11, 15]

Similarly, the idea of map function can be used to the cluster server, that is, to cut a lot of data, and then put each piece of data into different computers for parallel processing, and are the same mapping relationship of the calculation, the number of data has not increased or decreased. Then the processed data is then lumped together for the reduce process.

How does the reduce () function in Python work? We can learn from this article.

Summarize

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.