Amount in python

Is there a built-in function in python, numpy or one of its libraries can get the sum of series like this:

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

enter image description here

where x and y are lists, and L is the length of x or y.

Finally, if there is no built-in function, do it, I tried another code, for example:

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

Of course, this does not work, but I need something next to this or next to this:

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

Note. I can create my own function for this, but I'm looking for a simple, fast or inline, so please do not provide me with your own function.

Edit: I need a general form so that I can use this form if it has an example or another kind of math . Log(n)

enter image description here

+4
source share
5 answers

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/whatever 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) )
+4

:

result = numpy.dot(list1, list2)

, NumPy, . NumPy .

+9

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
+1

np.einsum

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

np.inner

np.inner(list1, list2)

np.dot

np.dot(list1, list2)

27

For large arrays np.einsumbegins to see increased performance

enter image description here

+1
source

Use the operator *and sum it along the 0ith axis.

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

But you really have to use a lot cooler and ultrafast np.inner()

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

Source: https://habr.com/ru/post/1674987/


All Articles