Python map()函数

MapReduce: Simplified Data Processing on Large Clusters
Jeffrey Dean and Sanjay Ghemawat
https://research.google.com/archive/mapreduce.html
这篇来自谷歌的论文介绍了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.

简而言之,map()和reduce()是在集群式设备上用来做大规模数据处理的方法,用户定义一个特定的映射,函数将使用该映射对一系列键值对进行处理,直接产生一系列键值对。

Python map()函数

Python可以接收函数作为参数。

map()是Python内置的高级函数之一,该函数可以接受其他函数作为参数,对一个序列的所有元素做该函数的映射,返回处理结果的一个map类型的对象。

同iterator,map也是一个可迭代对象。

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(i)
print(list(i))  #map是惰性序列,使用list计算出其所有元素的值

#结果:
<map object at 0x035E2190>
[1, 4, 9, 16, 25]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值