pythonic减少与累加和任意lambda函数? [英] Pythonic reduce with accumlation and arbitrary lambda function?

查看:131
本文介绍了pythonic减少与累加和任意lambda函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用累积的方式进行归约的Pythonic方法是什么?

例如,以R此答案):

Reduce(`*`, x=list(5,4,3,2), accumulate=TRUE)
# [1]   5  20  60 120

很重要的一点是,可以使用任意的lambda函数(例如lambda x, y: ...),因此允许使用例如仅使用总和,乘法或其他方法将无法解决问题.我无法提出一个Pythonic解决方案来做到这一点,例如Python的 itertools 解决方案

在Python 3中(在3.2中引入,可以传递在3.3中添加的功能)在此处找到完整列表.. >

每个numpy.ufunc都有 accumulate 方法,因此您可以执行以下操作:

import numpy as np
np.multiply.accumulate([5, 4, 3, 2])
# array([  5,  20,  60, 120])

What would be the Pythonic way of performing a reduce with accumulation?

For example, take R's Reduce(). Given a list and an arbitrary lambda function it allows for yielding a vector of accumulated results instead of only the final result by setting accumulate=T. An example for this with a simple multiplication as lambda function would be (taken from this answer):

Reduce(`*`, x=list(5,4,3,2), accumulate=TRUE)
# [1]   5  20  60 120

It is important that an arbitrary lambda function (like lambda x, y: ...) can be used, so solutions that allow for e.g. only using a sum, multiplication, or else won't do the trick. I was not able to come up with a Pythonic solution to do this with e.g. Python's itertools or functools, but there might be a way. And though there are numerous other questions and answers on reduce and specially accumulation with Python I did not spot a generic answer so far.

A non-Pythonic example using a loop for performing a accumulated reduce with an arbitrary lambda function could look like this:

# the source list
l = [0.5, 0.9, 0.8, 0.1, 0.1, 0.9]
# the lambda function for aggregation can be arbitrary
# this one is just made up for the example
func = lambda x, y: x * 0.65 + y * 0.35 

# the accumulated reduce:
# a) the target list with initializer value hardcoded
l2 = [l[0]]
# b) the loop
for i in range(1, len(l)):
    l2 += [func(
            l2[i-1],    # last value in l2
            l[i]        # new value from l   
            )]

So: how would you do a reduce with accumulation and arbitrary lambda function in a Pythonic way?

解决方案

In Python 3 (introduced in 3.2, ability to pass the function added in 3.3) this is already implemented, in itertools.accumulate. Just use it like this:

from itertools import accumulate
list(accumulate([5, 4, 3, 2], lambda a, b: a*b))
# [5, 20, 60, 120]


If you are using an earlier Python version, or want to implement it yourself, and you really want any arbitrary lambda (that takes two arguments) to work, then you could use the generator which is given in the documentation of the above:

def accumulate(iterable, func=operator.add):
    'Return running totals'
    # accumulate([1,2,3,4,5]) --> 1 3 6 10 15
    # accumulate([1,2,3,4,5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    for element in it:
        total = func(total, element)
        yield total

The usage is exactly the same as above.


If you are using numpy, then there exists a faster solution, at least for all numpy.ufuncs. These include basically the same functions that the standard library module math provides, and then some. You can find a complete list here.

Every numpy.ufunc has the accumulate method, so you can just do:

import numpy as np
np.multiply.accumulate([5, 4, 3, 2])
# array([  5,  20,  60, 120])

这篇关于pythonic减少与累加和任意lambda函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆