python的map,reduce,filter用法举例
2013-08-09 21:29
459 查看
对一个列表[1,3,5,7,8,9,4] 用map返回乘方列表;reduce计算乘积、filter挑出奇数
a=[1, 3, 5, 7, 8, 9, 4] def func_1(x): """ 返回乘方列表 """ return x*x def func_2(x, y): """ 返回乘积 """ return x*y def func_3(x): """ 挑出奇数 """ if (x%2 != 0): return x if __name__ == '__main__': print "main()" # 用map返回乘方列表 b = map(func_1, a) print "类型:%s 输出: %s"%(type(b), b) # reduce计算乘积 b = reduce(func_2, a); print "类型:%s 输出: %s"%(type(b), b) # filter挑出奇数 b = filter(func_3, a) print "类型:%s 输出: %s"%(type(b), b) # sorted排序反序输出 b = sorted(a,reverse=True) print "类型:%s 输出: %s"%(type(b), b)输出结果:
main() 类型:<type 'list'> 输出: [1, 9, 25, 49, 64, 81, 16] 类型:<type 'int'> 输出: 30240 类型:<type 'list'> 输出: [1, 3, 5, 7, 9] 类型:<type 'list'> 输出: [9, 8, 7, 5, 4, 3, 1]
相关文章推荐
- Python的map/filter/reduce常见用法例举
- python特殊用法:filter map reduce lambda
- 【Python】Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- Python中 filter | map | reduce | lambda的用法
- 转 python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- 关于python中,map,reduce,filter,sorted函数的用法
- Python学习笔记(5):lambda,filter,map,reduce,zip用法整理
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法
- python中的map、reduce、filter用法
- Python中filter、map、reduce、lambda 的用法
- Python中filter、map、reduce、lambda 的用法
- python之lambda、filter、map、reduce的用法说明
- python filter/map/reduce的用法
- python基础===map, reduce, filter的用法
- 关于python中,map,reduce,filter,sort函数的用法:
- Python一些特殊用法(map、reduce、filter、lambda、列表推导式等)
- python 的几个内置函数(lambda ,zip, filter, map, reduce )用法