python中的reduce函数
来源:互联网 发布:京东自动抢购软件 编辑:程序博客网 时间:2024/05/18 10:25
Python中的reduce
python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是一个二元操作函数)先对集合中的第1,2个数据进行操作,得到的结果再与第三个数据用func()函数运算,最后得到一个结果。
如:
def myadd(x,y):
return x+y
sum=reduce(myadd,(1,2,3,4,5,6,7))
print sum
#结果就是输出1+2+3+4+5+6+7的结果即28
当然,也可以用lambda的方法,更为简单:
sum=reduce(lambda x,y:x+y,(1,2,3,4,5,6,7))
print sum
在python 3.0.0.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce
阅读全文
0 0
- python中的reduce函数
- python中的reduce函数
- python中的reduce函数
- python中的reduce函数
- python中的reduce函数
- python中的reduce()函数
- python中的reduce函数
- python中的reduce函数
- python中的reduce函数
- python中的map、filter、reduce函数
- python中的map、filter、reduce函数
- python中的map、filter、reduce函数
- python中的map、filter、reduce函数
- python中的map、filter、reduce函数
- python中的map、filter、reduce函数
- python中的函数:filter/map/reduce/lambda
- python中的map、filter、reduce函数
- python中的map, filter, reduce函数
- 《Kotlin极简教程》第2章 快速开始:HelloWorld
- 《Kotlin极简教程》第3章 Kotlin语言基础
- RelativeLayout无法充满ScrollView
- JVM 内存模型 Stack Heap 文章选摘
- 《Kotlin极简教程》第3章 Kotlin语言基础 配图
- python中的reduce函数
- 《Kotlin 极简教程 》第4章 基本数据类型与类型系统
- Koltin Any 类型
- js-邮箱验证
- 《Kotlin 极简教程 》第5章 集合类(1)
- 《Kotlin 极简教程 》第6章 泛型
- 《Kotlin 极简教程 》第5章 集合类(2)
- 初谈对微服务的了解
- 《Kotin 极简教程》第7章 面向对象编程(OOP)(1)