python: reduce函数
来源:互联网 发布:ubuntu 字体 编辑:程序博客网 时间:2024/06/04 22:53
API
reduce(function, sequence, starting_value)
顺序迭代。
可设置初始值。
实验代码
未设置 初始值
则 默认 以 sequence[0] 作为 初始值
list = [2, 3, 4]f = lambda x, y : x * yassert reduce(f, list) == 2*3*4
设置了 初始值
list = [2, 3, 4]f = lambda x, y : x * yassert reduce(f, list, 10) == 10*2*3*4
寻找最大值
list = [10, 100, 70, 20]assert reduce(lambda a,b : a if a>b else b, list) == 100
也可以用python 自带的 max函数:
list = [10, 100, 70, 20]assert reduce(max, list) == 100
等同于:
list = [10, 100, 70, 20]assert max(list) == 100
求平方和
list = [2, 3, 4]assert reduce(lambda x,y : x+y**2, list, 0) == 2**2 + 3**2 + 4**2
阅读全文
0 0
- python中的reduce函数
- Python reduce函数
- python中的reduce函数
- python中的reduce函数
- python reduce函数
- python中的reduce函数
- python中的reduce函数
- Python reduce函数
- Python-reduce函数
- python中reduce()函数
- python中的reduce()函数
- python的reduce()函数
- Python--reduce()函数
- python中的reduce函数
- python中reduce()函数
- reduce函数python
- python的reduce()函数
- python中的reduce函数
- IEnumerator 和IEnumerable区别
- 高效App框架设计与重构
- MySQL链接
- [agc017F]Zigzag
- Filter(第三节)
- python: reduce函数
- 列表表达式实例
- Action和Page数据共享方式 (值栈/Context) 值栈获取方式
- Search for a Range--LeetCode
- C++ 数论-质因数分解
- Struts 2 访问session request application 对象的方式
- Mapping the request body with the @RequestBody annotation
- define(宏) const(常变量) enum(枚举)
- [noip2012]疫情控制(二分+贪心)