python 函数 reduce、filter
## reduce
>>> reduce(lambda x,y: x+y,[1,2,3,4,5]) 15
请看官仔细观察,是否能够看出是如何运算的呢?画一个图:
还记得map是怎么运算的吗?忘了?看代码:
>>> list1 = [1,2,3,4,5,6,7,8,9] >>> list2 = [9,8,7,6,5,4,3,2,1] >>> map(lambda x,y: x+y, list1,list2) [10, 10, 10, 10, 10, 10, 10, 10, 10]
对比一下,就知道两个的区别了。原来map是上下运算,reduce是横着逐个元素进行运算。
权威的解释来自官网:
reduce(function, iterable[, initializer])
Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates ((((1+2)+3)+4)+5). The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable. If the optional initializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty. If initializer is not given and iterable contains only one item, the first item is returned. Roughly equivalent to:
def reduce(function, iterable, initializer=None): it = iter(iterable) if initializer is None: try: initializer = next(it) except StopIteration: raise TypeError('reduce() of empty sequence with no initial value') accum_value = initializer for x in it: accum_value = function(accum_value, x) return accum_value
如果用我们熟悉的for循环来做上面reduce的事情,可以这样来做:
>>> lst = range(1,6) >>> lst [1, 2, 3, 4, 5] >>> r = 0 >>> for i in range(len(lst)): ... r += lst[i] ... >>> r 15
for普世的,reduce是简洁的。
为了锻炼思维,看这么一个问题,有两个list,a = [3,9,8,5,2],b=[1,4,9,2,6],计算:a[0]b[0]+a[1]b[1]+...的结果。
>>> a [3, 9, 8, 5, 2] >>> b [1, 4, 9, 2, 6] >>> zip(a,b) #复习一下zip,下面的方法中要用到 [(3, 1), (9, 4), (8, 9), (5, 2), (2, 6)] >>> sum(x*y for x,y in zip(a,b)) #解析后直接求和 133 >>> new_list = [x*y for x,y in zip(a,b)] #可以看做是上面方法的分布实施 >>> #这样解析也可以:new_tuple = (x*y for x,y in zip(a,b)) >>> new_list [3, 36, 72, 10, 12] >>> sum(new_list) #或者:sum(new_tuple) 133 >>> reduce(lambda sum,(x,y): sum+x*y,zip(a,b),0) #这个方法是在耍酷呢吗? 133 >>> from operator import add,mul #耍酷的方法也不止一个 >>> reduce(add,map(mul,a,b)) 133 >>> reduce(lambda x,y: x+y, map(lambda x,y: x*y, a,b)) #map,reduce,lambda都齐全了,更酷吗? 133
filter
filter的中文含义是“过滤器”,在python中,它就是起到了过滤器的作用。首先看官方说明:
filter(function, iterable)
filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。
该接收两个参数,第一个为函数,第二个为序列,序列的每个元素作为参数传递给函数进行判,然后返回 True 或 False,最后将返回 True 的元素放到新列表中。
通过下面代码体会:
>>> numbers = range(-5,5) >>> numbers [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4] >>> filter(lambda x: x>0, numbers) [1, 2, 3, 4] >>> [x for x in numbers if x>0] #与上面那句等效 [1, 2, 3, 4] >>> filter(lambda c: c!='i', 'hiekay') #能不能对应上面文档说明那句话呢? 'hekay' #“If iterable is a string or a tuple, the result also has that type;”
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Pytorch入门演练
【引言】Pytorch是一个基于Python的科学计算软件包,有以下两种定位: 可以使用多GPU加速的NumPy替代品 提供最大限度灵活性与速度的深度学习研究平台 一、入门 1.Tensors(张量) Tensors(张量)类似于NumPy中的ndarray,另外它还可以使用GPU加速计算。 from__future__import print_function importtorch 构造一个未初始化的5x3矩阵: x = torch.empty(5, 3) print(x) 输出: tensor([[-9.0198e-17, 4.5633e-41, -2.9021e-15], [ 4.5633e-41, 0.0000e+00, 0.0000e+00], [ 0.0000e+00, 0.0000e+00, 0.0000e+00], [ 0.0000e+00, 0.0000e+00, 0.0000e+00], [ 0.0000e+00, 0.0000e+00, 0.0000e+00]]) 构造一个随机初始化的矩阵: x = torch.rand(5, 3) print(x) 输出: t...
- 下一篇
TLV格式 及 VARINT数值压缩存储方法
使用Thrift格式进行数据序列化反序列化,thrift的存储格式,主要使用thrift的TCompactProtocol。 发现该序列化方式主要使用了TLV格式式来存储每个字段,使用VARINT来表示其中的L。 1. TLV 格式 很简单,Type-length-value(类型-长度-值)。在一串字节中,使用该方式标示出一个自定义的字段。 三个域的表示方式均可自定义。如使用1个字节标示数据类型T,使用4个字节标示数据长度L,之后使用L个字节来表示数据的值。(其实Thrift的TBinaryProtocol就是这种方式) 2. VARINT 数值压缩存储 c中int使用4个固定字节表式,即使数字很小。假如使用int16,则不能表示较大的数字。 而VARINT是一种可变长度的表示数字的方法,当数字较小时可以使用1个字节,如果比较大需要利用5个。
相关文章
文章评论
共有0条评论来说两句吧...