python 3.0以后, reduce已经不在built-in function里了, 要用它就得from functools import reduce.

reduce的用法

reduce(function, sequence[, initial]) -> value

Apply a function of two arguments cumulatively to the items of a sequence,
from left to right, so as to reduce the sequence to a single value.
For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates
((((1+2)+3)+4)+5).  If initial is present, it is placed before the items
of the sequence in the calculation, and serves as a default when the
sequence is empty.

意思就是对sequence连续使用function, 如果不给出initial, 则第一次调用传递sequence的两个元素, 以后把前一次调用的结果和sequence的下一个元素传递给function. 如果给出initial, 则第一次传递initial和sequence的第一个元素给function.

from functools import reduce
reduce(lambda x,y: x+y, [1, 2, 3])
输出 6
reduce(lambda x, y: x+y, [1,2,3], 9)
输出 15
reduce(lambda x,y: x+y, [1, 2, 3], 7)
输出 13

  

*functool标准库还有很多功能,可以参考网上的资料

最新文章

  1. String的方法
  2. 关于display:flex
  3. AngularJs angular.Module模块接口配置
  4. MvcAdmin功能介绍
  5. [设计模式] 17 中介者模式 Mediator Pattern
  6. sunlime text 3 快捷键总结
  7. 华为机试_字符串识别_Vector的使用;
  8. c#学习心得,慢慢添加,如果有错误希望大家留言,我刚开始学
  9. Oracle EBS Web ADI 中的术语
  10. Core Animation中的基础动画
  11. JavaScript If…Else 语句
  12. JavaScript函数式编程
  13. Codeforces 965E Short Code 启发式合并 (看题解)
  14. Const的使用
  15. sublime格式化js、css、html的通用插件-html js css pretty
  16. 【NOI2000】 单词查找树
  17. RV32A指令集
  18. preg_match用法
  19. Hive 复制分区表和数据
  20. 《区块链100问》第82集:应用类项目Golem

热门文章

  1. Linux - wc统计文件行数、单词数或字节数
  2. iOS开发——UI篇Swift篇&UIScrollView
  3. mkinitrd 与 mkinitramfs
  4. Windows 之 删除保存的共享凭据(用户名和密码)
  5. Android中GridLayout与GridView区别
  6. c#入门实例
  7. Objective-C 高级编程:iOS与OS X多线程和内存管理
  8. IIS 7.5站点配置
  9. umbraco表单
  10. poj 2029 二维树状数组