Python map()函数

本文章向大家介绍Python map()函数,主要内容包括Python map()函数、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

MapReduce: Simplified Data Processing on Large Clusters Jeffrey Dean and Sanjay Ghemawat 这篇来自谷歌的论文介绍了map/reduce,摘录如下:

Abstract MapReduce is a programming model and an associated implementation for processing and generating large data sets. Users specify a map function that processes a key/value pair to generate a set of intermediate key/value pairs, and a reduce function that merges all intermediate values associated with the same intermediate key. Many real world tasks are expressible in this model, as shown in the paper. Programs written in this functional style are automatically parallelized and executed on a large cluster of commodity machines. The run-time system takes care of the details of partitioning the input data, scheduling the program’s execution across a set of machines, handling machine failures, and managing the required inter-machine communication. This allows programmers without any experience with parallel and distributed systems to easily utilize the resources of a large distributed system. Our implementation of MapReduce runs on a large cluster of commodity machines and is highly scalable: a typical MapReduce computation processes many terabytes of data on thousands of machines. Programmers find the system easy to use: hundreds of MapReduce programs have been implemented and upwards of one thousand MapReduce jobs are executed on Google’s clusters every day.


Python map()函数





map(function, iterable, ...)   #意即可接超过2个参数

形式:map(函数, 可迭代对象) 其语法意义就是,是用给出的函数对所有可迭代对象进行处理,返回一个map类型的对象,请注意,Java中map是键值对,相当于Python的dict,而Python中的map是一个类似与list的数据类型,不是键值对。

#!usr/bin/env python3
#_*_ coding: utf-8 _*_
def square(x):
    return x * x
i =  map(square, range(1,6))
print(list(i))  #map是惰性序列,使用list计算出其所有元素的值

<map object at 0x035E2190>
[1, 4, 9, 16, 25]