计算点积的Pythonic方法是什么?

发布于 2024-11-05 07:39:25 字数 209 浏览 0 评论 0原文

我有两个列表,一个名为 A,另一个名为 B。A 中的每个元素都是一个三元组,B 中的每个元素只是一个数字。我想计算的结果定义为:

result = A[0][0] * B[0] + A[1][0] * B[1] + ... + A[n-1][0] * B[n-1]

我知道逻辑很简单,但如何以Pythonic方式编写?

谢谢!

I have two lists, one is named as A, another is named as B. Each element in A is a triple, and each element in B is just an number. I would like to calculate the result defined as :

result = A[0][0] * B[0] + A[1][0] * B[1] + ... + A[n-1][0] * B[n-1]

I know the logic is easy but how to write in pythonic way?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

软的没边 2024-11-12 07:39:25

Python 3.5 有一个用于点积的显式运算符 @
所以你可以写

a = A @ B

而不是

a = numpy.dot(A,B)

Python 3.5 has an explicit operator @ for the dot product,
so you can write

a = A @ B

instead of

a = numpy.dot(A,B)
给我一枪 2024-11-12 07:39:25
import 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))] )
import numpy
result = numpy.dot( numpy.array(A)[:,0], B)

http://docs.scipy.org/doc/numpy/reference/

If you want to do it without numpy, try

sum( [a[i][0]*b[i] for i in range(len(b))] )
琴流音 2024-11-12 07:39:25

我最喜欢的 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)])

My favorite Pythonic dot product is:

sum([i*j for (i, j) in zip(list1, list2)])

So for your case we could do:

sum([i*j for (i, j) in zip([K[0] for K in A], B)])
一袭水袖舞倾城 2024-11-12 07:39:25
from operator import mul

sum(map(mul, A, B))
from operator import mul

sum(map(mul, A, B))
亚希 2024-11-12 07:39:25

使用运算符和 itertools 模块:

from operator import mul
from itertools import imap

sum(imap(mul, A, B))

Using the operator and the itertools modules:

from operator import mul
from itertools import imap

sum(imap(mul, A, B))
要走就滚别墨迹 2024-11-12 07:39:25

使用more_itertools,一个实现dotproduct itertools 配方

import more_itertools as mit


a = [1, 2, 3]
b = [7, 8, 9]

mit.dotproduct(a, b)
# 50

Using more_itertools, a third-party library that implements the dotproduct itertools recipe:

import more_itertools as mit


a = [1, 2, 3]
b = [7, 8, 9]

mit.dotproduct(a, b)
# 50
伴我老 2024-11-12 07:39:25

人们正在将 @ 运算符重新指定为点积运算符。这是我的代码,使用普通 python 的 zip 返回一个元组。然后使用列表理解而不是映射。

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 

People are re-assigning the @ operator as the dot product operator. Here's my code using vanilla python's zip which returns a tuple. Then uses list comprehension instead of 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 
动次打次papapa 2024-11-12 07:39:25
>>> 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

就是这样。

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

And that's it.

平定天下 2024-11-12 07:39:25

在 Python 3.12+ 中,您可以使用 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

在旧版本中,最Pythonic的方法可能是使用使用生成器求和(即不构建中间列表):

>>>  sum(row[0] * b for row, b in zip(A, B, strict=True))
210

In Python 3.12+ you can use math.sumprod.

Given your situation

A = [
    (1, 11, 111),
    (2, 22, 222),
]
B = [10, 100]

first_column = (row[0] for row in A)

you can compute A[0][0] * B[0] + A[1][0] * B[1] with

>>> import math
>>> math.sumprod(first_column, B)
210

In older versions the most pythonic way is probably to use sum with a generator (i.e., without building an intermediate list):

>>>  sum(row[0] * b for row, b in zip(A, B, strict=True))
210
笑忘罢 2024-11-12 07:39:25

对于这种事情来说,最Pythonic的方法可能是使用 numpy< /a>. ;-)

Probably the most Pythonic way for this kind of thing is to use numpy. ;-)

趁微风不噪 2024-11-12 07:39:25

但是,这可能是重复的解决方案:

>>> u = [(1, 2, 3), (4, 5, 6)]
>>> v = [3, 7]

在普通 Python 中:

>>> sum([x*y for (x, *x2), y in zip(u,v)])
31

或使用 numpy (如 用户57368的回答):

import numpy as np
>>> np.dot(np.array(u)[:,0], v)
31

This might be repeated solution, however:

>>> u = [(1, 2, 3), (4, 5, 6)]
>>> v = [3, 7]

In plain Python:

>>> sum([x*y for (x, *x2), y in zip(u,v)])
31

Or using numpy (as described in user57368's answer) :

import numpy as np
>>> np.dot(np.array(u)[:,0], v)
31
我偏爱纯白色 2024-11-12 07:39:25

以上所有答案都是正确的,但在我看来,计算点积的最Pythonic方法是:

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> sum(map(lambda pair:pair[0]*pair[1],zip(a,b)))
32

All above answers are correct, but in my opinion the most pythonic way to calculate dot product is:

>>> a=[1,2,3]
>>> b=[4,5,6]
>>> sum(map(lambda pair:pair[0]*pair[1],zip(a,b)))
32
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文