如何在python中实现“cumdot”

2024-10-01 07:11:37 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我有两个长度相同的1d数组(比如说n),现在我想实现一个“cumdot”函数,它输出长度为n的1d数组,可以用纯python代码实现

def cumdot(a,b):#a,b are two 1d arrays with same length
    n = len(a)
    output = np.empty(n)
    for i in range(n):
        output[i] = np.dot(a[:i+1],b[:i+1])
    return output

如何更有效地实现“cumdot”功能?你知道吗


Tags: 函数代码outputlendefwithnp数组
2条回答
def cumdot(a, b):
    return numpy.cumsum(a * b)

我认为纯python的意思是:使用python的核心!

def comdot(a, b):
    result = [i[0]*i[1] for i in zip(a, b)]
    return result

相关问题 更多 >