python中reduce用法

2015-01-23 0 381
python中reduce用法
>>> def myfunction(a,b):
...     return a*b
...
>>> mylist = [1,2,3,4,5]
>>> print reduce(myfunction, mylist)
120
 
#上面的代码相当于:
>>>print ((((1*2)*3)*4)*5)
120
 
#也可以直接使用操作符模块来替代函数
>>> import operator
>>> mylist = [1,2,3,4,5]
>>> print reduce(operator.mul, mylist)
120
>>> print reduce(operator.add, mylist)
15

遇见资源网 python python中reduce用法 http://www.ox520.com/15063.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务