我有两个列表,一个叫做A,另一个叫做B。 A中的每个元素都是三元组,而B中的每个元素都只是一个数字。我想计算定义为:
result = A[0][0] * B[0] + A[1][0] * B[1] + ... + A[n-1][0] * B[n-1]
我知道逻辑很简单,但如何用Pythonic的方式编写呢?
谢谢!
我有两个列表,一个叫做A,另一个叫做B。 A中的每个元素都是三元组,而B中的每个元素都只是一个数字。我想计算定义为:
result = A[0][0] * B[0] + A[1][0] * B[1] + ... + A[n-1][0] * B[n-1]
我知道逻辑很简单,但如何用Pythonic的方式编写呢?
谢谢!
Python 3.5 版本引入了显式的运算符@
来执行点积运算,因此您可以这样编写代码
a = A @ B
取代
a = numpy.dot(A,B)
@
是 Python 的符号,尽管它目前并未使用。Numpy 在其数组中实现了该符号。 - GeeTransit@
运算符是专门用于矩阵乘法的,因此 numpy 并没有胡乱发明:https://www.python.org/dev/peps/pep-0465/ (只是基本的 Python 没有矩阵而已) - remram[1,2,3] @ [2,1,1]
应该可以工作,但实际上并不行。 - run_the_raceimport numpy
result = numpy.dot( numpy.array(A)[:,0], B)
http://docs.scipy.org/doc/numpy/reference/
如果想不使用numpy进行操作,请尝试
sum( [a[i][0]*b[i] for i in range(len(b))] )
我最喜欢的Pythonic点积是:
sum([i*j for (i, j) in zip(list1, list2)])
对于您的情况,我们可以这样做:
sum([i*j for (i, j) in zip([K[0] for K in A], B)])
from operator import mul
sum(map(mul, A, B))
使用operator和itertools模块:
from operator import mul
from itertools import imap
sum(imap(mul, A, B))
使用第三方库more_itertools
,它实现了dotproduct
itertools recipe:
import more_itertools as mit
a = [1, 2, 3]
b = [7, 8, 9]
mit.dotproduct(a, b)
# 50
人们正在重新分配@运算符作为点积运算符。这是使用纯Python的zip函数返回元组的代码。然后使用列表推导式代替map函数。
def dot_product(a_vector,b_vector):
#a1 x b1 + a2 * b2..an*bn return scalar
return sum([an*bn for an,bn in zip(a_vector,b_vector)])
X = [2,3,5,7,11]
Y = [13,17,19,23,29]
print(dot_product(X,Y)) #652
a=[1,2,3]
b=[4,5,6]
print(dot_product(a,b)) #prints 32= 1*4 + 2*5 + 3*6 =
a = [1, 2, 3]
b = [7, 8, 9]
print(dot_product(a,b)) #prints 50
>>> X = [2,3,5,7,11]
>>> Y = [13,17,19,23,29]
>>> dot = lambda X, Y: sum(map(lambda x, y: x * y, X, Y))
>>> dot(X, Y)
652
就是这样。
math.sumprod
。A = [
(1, 11, 111),
(2, 22, 222),
]
B = [10, 100]
first_column = (row[0] for row in A)
A[0][0] * B[0] + A[1][0] * B[1]
进行计算。>>> import math
>>> math.sumprod(first_column, B)
210
sum
函数一起使用。>>> sum(row[0] * b for row, b in zip(A, B, strict=True))
210
A
的结构是一个二维列表/矩阵。 - igauravsehrawat