python之map和reduce的区别
来源:互联网 发布:mysql高级教程 编辑:程序博客网 时间:2024/04/29 22:19
①从参数方面来讲:
map()函数:
map()包含两个参数,第一个是参数是一个函数,第二个是序列(列表或元组)。其中,函数(即map的第一个参数位置的函数)可以接收一个或多个参数。
reduce()函数:
reduce() 第一个参数是函数,第二个是 序列(列表或元组)。但是,其函数必须接收两个参数。
②从对传进去的数值作用来讲:
map()是将传入的函数依次作用到序列的每个元素,每个元素都是独自被函数“作用”一次;(请看下面的栗子)
reduce()是将传人的函数作用在序列的第一个元素得到结果后,把这个结果继续与下一个元素作用(累积计算),
最终结果是所有的元素相互作用的结果。(请看下面的栗子)
举个栗子:
map()函数:
# 传入一个参数def one_p(x): return x * xprint 'map1.1:', map(one_p, range(1, 5)) #结果:map1.1: [1, 4, 9, 16]print 'map1.2:', map(one_p, [1, 2, 3, 4, 5, 6])#结果:map1.2: [1, 4, 9, 16, 25, 36]# 传入多个参数a = [1, 2, 3, 4, 5]b = [1, 1, 6, 2, 3]c = [1, 2, 3, 4, 5]s = map(lambda (x, y, z): x * y * z, zip(a, b, c))print 'map2:', s#结果:map2: [1, 4, 54, 32, 75]
reduce()函数:
r1 = reduce(lambda x, y: x * y, (2, 2, 6, 2)) #运算过程:(((2*2)*6)*2)r2 = reduce(lambda x, y: x * y, (2, 2, 6), 2) #<span style="font-family: Arial, Helvetica, sans-serif;">运算过程:(((2*2)*6)*2)</span>print 'r1:', r1 # 结果:r1: 48print 'r2:', r2 # 结果:r2: 48
0 0
- python之map和reduce的区别
- python中map()和reduce()函数的区别
- python之map/reduce
- Python之map()/reduce()
- Map Join和Reduce Join的区别
- Python函数Map()和reduce()的区别和第二天学习Python的心得体会
- python map和reduce的用法
- python map和reduce的用法
- python map和reduce的用法
- python map和reduce的用法
- python的reduce函数和map函数
- python的map和reduce函数
- python 学习--map 和 reduce的使用
- python中map和reduce的应用
- Python 中 map和reduce 的用法
- python的map,reduce和zip
- python中map,filter和reduce函数区别
- Python之map、reduce、filter
- ios10调用相机崩溃!(设置权限)
- window环境下定 时备份 oracle数据
- CGContextTranslateCTM
- 【原创】【NOIP】海港的船只
- shell之正则表达式
- python之map和reduce的区别
- 了解认识泛型
- 23种设计模式
- jr-ide-intellij-6.5.2_13-16.zip 下载
- RxJava+Retrofit+OkHttp深入浅出-终极封装六特殊篇(变种String替换Gson自由扩展)
- Codeforces Round #378 (Div. 2)A. Grasshopper And the String
- js预加载图片
- 谷歌浏览器扩展程序-实现按钮自动连续点击
- 长按tableview弹出相关菜单