系列总和在python中

时间:2017-04-17 17:35:53

标签: python numpy

python中是否有内置函数,numpy或其中一个库可以得到像这样的系列之和:

list1 = [2,3,4]
list2 = [3,3,3]

enter image description here

其中x和y是列表,L是x或y的长度。

最后,如果没有内置函数,我尝试了另一个代码,如:

Total = sum ((i for i in list1) * (j for j in list2))

当然,它不起作用,但我需要靠近这个或附近的东西:

Total = sum (i * j for i in list1 and j in list2 )

注意:我可以建立自己的功能,但我正在寻找一个简单,快速或内置的功能,所以请不要给我自己的功能。

编辑:我希望通用表单能够这样做,因此当系列中有 Log(n)或其他类型的数学时,我可以使用此表单。

enter image description here

5 个答案:

答案 0 :(得分:9)

这实际上只是一个点积:

result = numpy.dot(list1, list2)

请注意,如果您使用的是NumPy,则不应使用列表来表示矩阵和向量。 NumPy数组更加高效和方便。

答案 1 :(得分:4)

对于内置,您可以使用zip将相同索引位置的元素组合在一起

list1 = [2,3,4]
list2 = [3,3,3]
result = sum( x*y for x,y in zip(list1, list2) )

关于修改

内置版本将是

from math import log
result = sum( log(i)*y for i,y in enumerate(list1,1) )

更通用的版本将是

enter image description here

import operator
def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul):
    return sum(map(mul, vec1, vec2))    

您可以在其中提供您喜欢的任何功能,然后第一个是

result = dotproduct(list1,list2)

,第二个可能是

result = dotproduct(range(1,len(list1)+1),list1, mul=lambda i,x:log(i)*x )
#                        ^ the i                    ^ how to operate

result = dotproduct(map(log,range(1,len(list1)+1) ), list1 )
#                           ^ the log i

关键是你计算第二个向量

使用numpy更容易

import numpy as np
logi = np.log(np.arange(1,len(list1)+1)
result = np.dot(logi,list1)

再次归结为相应地计算零件

你也可以这样做,而不是接收2个向量/列表,它只使用一个并接收一个在元素及其索引中起作用的函数

enter image description here

def sum_serie(vect, fun = lambda i,x:x, i_star=0): #with that fun, is like the regular sum
    return sum( fun(i,x) for i,x in enumerate(vect, i_star) )

并将其用作

result = sum_serie( list1, lambda i,x:log(i)*x, 1)

从评论中,如果我做对了,那就是这样的

from itertools import islice
def sum_serie(vect, *slice_arg, fun = lambda x:x): #with that fun, is like the regular sum
    """sum_serie(vect, [start,]stop[,step], fun = lambda x:x)"""
    if not slice_arg:
        slice_arg = (0,None,None)
    return sum( fun(x) for x in islice(vect, *slice_arg) )

或以前枚举

from itertools import islice
def sum_serie_i(vect, *slice_arg, fun = lambda i,x:x): #with that fun, is like the regular sum
    if not slice_arg:
        slice_arg = (0,None,None)
    return sum( fun(i,x) for i,x in islice(enumerate(vect), *slice_arg) )

并使用,例如

sum_serie( x, 0, 100, 2, fun=lambda xi: c*xi) #for arbitrary constant c
sum_serie_i( x, 0, 100, 2, fun=lambda i,xi: log(i)*xi)

注意:这样它接受serie / iterable / what和最多3个位置参数,其含义与范围

相同

注意2:对于PY3而言,它只是一个关键字参数,在python 2中,使用

完成相同的效果
def sum_serie_i(vect, *slice_arg, **kargv): 
    fun = kargv.get('fun', lambda i,x:x) #with that fun, is like the regular sum
    if not slice_arg:
        slice_arg = (0,None,None)
    return sum( fun(i,x) for i,x in islice(enumerate(vect), *slice_arg) )

答案 2 :(得分:1)

使用numpy数组的数学运算符执行元素操作,因此您可以使用:

import numpy as np
np.sum(np.multiply(list1, list2))
# or np.sum(list1 * list2)  if any of list1/list2 is a numpy array

或在第二种情况下:

logi = np.log(np.arange(1, len(x)))  # or np.log10, not sure which one you mean
np.sum(np.multiply(logi, x[1:]))

如上所述np.sum(np.multiply())只是一个dotproduct,所以你也可以使用@(至少使用python 3.5+,否则使用np.dot):

np.asarray(list1) @ list2  # if list1/list2 are numpy arrays you don't need the np.asarray

答案 3 :(得分:1)

这些都做同样的事情

np.einsum

np.einsum('i,i', list1, list2)

np.inner

np.inner(list1, list2)

np.dot

np.dot(list1, list2)

所有收益27

对于更大的阵列,np.einsum开始看到性能提升

enter image description here

答案 4 :(得分:0)

使用*运算符并沿0轴求和。

In [48]: a
Out[48]: array([0, 1, 2, 3, 4])

In [49]: b
Out[49]: array([5, 6, 7, 8, 9])

In [50]: a*b
Out[50]: array([ 0,  6, 14, 24, 36])

In [51]: np.sum(a*b, axis=0)
Out[51]: 80

但是,你应该真的使用更酷,超快np.inner()

# `a, b` can also be python lists
In [65]: np.inner(a, b)
Out[65]: 80