Python的functools.reduce用法
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标准库还有很多功能,可以参考网上的资料
Python的functools.reduce用法的更多相关文章
- python中filter,reduce,map的用法
filter的用法: 操作表list的内嵌函数'filter' 需要一个函数与一个list它用这个函数来决定哪个项应该被放入过滤结果队列中遍历list中的每一个值,输入到这个函数中如果这个函数返回Tr ...
- Python内建函数reduce()用法
reduce把一个函数作用在一个序列[x1, x2, x3...]上,这个函数必须接收两个参数,reduce把结果继续和序列的下一个元素做累积计算,下面讲述Python内建函数reduce()用法. ...
- python基础——map/reduce
python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...
- Python Lambda 的简单用法
下面代码简单举例介绍以下 lambda的用法. from functools import reduce #1 python lambda会创建一个函数对象,但不会把这个函数对象赋给一个标识符,而de ...
- 细说Python的lambda函数用法,建议收藏
细说Python的lambda函数用法,建议收藏 在Python中有两种函数,一种是def定义的函数,另一种是lambda函数,也就是大家常说的匿名函数.今天我就和大家聊聊lambda函数,在Pyth ...
- 函数fold 或reduce用法
http://yi-programmer.com/2011-02-24_fold.html http://c2.com/cgi/wiki?FoldFunction http://rwh.readthe ...
- python中filter(),reduce()函数
filter()函数 是 Python 内置的另一个有用的高阶函数,filter()函数接收一个函数 和一个list,这个函数的作用是对每个元素进行判断,返回 True或 False,filter() ...
- Python之利用reduce函数求序列的最值及排序
在一般将Python的reduce函数的例子中,通常都是拿列表求和来作为例子.那么,是否还有其他例子呢? 本次分享将讲述如何利用Python中的reduce函数对序列求最值以及排序. 我们用r ...
- Python中functools模块函数解析
Python自带的 functools 模块提供了一些常用的高阶函数,也就是用于处理其它函数的特殊函数.换言之,就是能使用该模块对可调用对象进行处理. functools模块函数概览 functool ...
随机推荐
- Android开发 设置开机自动启动
原文:http://blog.csdn.net/kevinmeng_ini58/article/details/7700786 片段一: <!-- 开机启动 --> <receive ...
- innodB的隐式锁
http://blog.csdn.net/taozhi20084525/article/details/19545231 一.知识准备之隐式锁 参考:http://www.uml.org.cn/sjj ...
- Linux内核学习笔记
1.vanbreaker的专栏 2.LinuxKernel Exploration 3.DroidPhone的专栏 4.Linux内核研究以及学习文档和ARM学习以及研究的开放文档 [力荐] 5. ...
- 更换VS2012序列号的方法
转自:http://blog.sina.com.cn/s/blog_58c506600101ja49.html 今天不小心把还在试用期内的VS2012 Ultimate填上盗版Key激活了……找遍了菜 ...
- 通过百度地图API将百度坐标转换成GPS经纬度
百度地图API链接:http://developer.baidu.com/map/index.php?title=webapi/guide/changeposition 百度地图API中,有GPS坐标 ...
- IDEA加密(转)
1. 简介 IDEA是International Data Encryption Algorithm 的缩写,是1990年由瑞士联邦技术学院来学嘉X.J.Lai 和Massey提出的建议标准算法称作P ...
- xml<>编译
< 用 < >用 > 表示"空格"
- 自定义带有uitableview的alertview对话框
#import <UIKit/UIKit.h> typedef void(^MyCompleteHandler) (NSString *selectString); @interface ...
- label
label的使用 以前只知道使用并没太注意一些细节 话说<label><input type="checkbox"/>5星</label>就可以 ...
- C# 枚举绑定到ComboBox
来自:http://blog.csdn.net/crazy_frog/article/details/7705442 方法一: 绑定 enum TestEnum {zero=0,one=1,two=2 ...