Python中两个列表的点积
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32669855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dot product of two lists in Python
提问by Benjamin Brooks
I need to write the function dot( L, K ) that should output the dot product of the lists L and K. If these two input lists are not of equal length, dot should output 0. If these two lists are both empty, dot also should output 0. You should assume that the input lists contain only numeric values.
我需要编写函数 dot( L, K ) 来输出列表 L 和 K 的点积。如果这两个输入列表长度不等,dot 应该输出 0。如果这两个列表都是空的,dot也应该输出 0。您应该假设输入列表只包含数值。
This is what I have so far:
这是我到目前为止:
def dot( L, K ):
if len[L]!=len[K]:
return 0
elif L == '' or L == []:
return 0
else:
return sum(L[0]*K[0], L[1]*K[1], ect.)
Can someone help me please because I can't figure out what to do in the last line!
有人可以帮我吗,因为我不知道最后一行要做什么!
采纳答案by VHarisop
You can do this using a list comprehension:
您可以使用列表理解来做到这一点:
def dot(K, L):
if len(K) != len(L):
return 0
return sum(i[0] * i[1] for i in zip(K, L))
If either of the lists is empty, zip(K, L)
will return []
. Then, by definition, sum([])
will give you zero.
如果其中一个列表为空,zip(K, L)
则返回[]
。然后,根据定义,sum([])
会给你零。
回答by Pablo Velázquez
The for loop returns an array that has each K*L element multiplied. Then the sum function adds each element and returns the dot product
for 循环返回一个数组,其中每个 K*L 元素都相乘。然后 sum 函数将每个元素相加并返回点积
def dot(K,L):
if len(K)==len(L) and len(K)!=0:
return sum([K[n]*L[n] for n in range(len(K))])
else:
return 0
回答by VDV
One liner which works for vectors of voluntary size (you may want to define it as a more regular and readable function or change the code to use sum
instead of leftmost reduce
). It does not define multiplication for non-equal lengths as it is not a part of standard dot product definition --it will just report an error on non-equal lengths:
一个适用于任意大小的向量的 liner(您可能希望将其定义为更规则和可读的函数,或者更改代码以使用sum
而不是 leftmost reduce
)。它没有定义不等长的乘法,因为它不是标准点积定义的一部分——它只会报告不等长的错误:
dotprod =lambda K, L:reduce(lambda z1, z2: z1+z2, map(lambda x: reduce(lambda x1, x2: x1*x2, x), zip(K, L)))
Quick test:
快速测试:
dotprod([1, 2, 3, 4], [5, 6, 7, 8])
Out[39]: 70
5+12+21+32
Out[40]: 70
if you still wish to incorporate the length checks and definition for non-equals multiplication:
如果您仍然希望合并非等乘法的长度检查和定义:
dotprod =lambda K, L: reduce(lambda z1, z2: z1+z2, map(lambda x: reduce(lambda x1, x2: x1*x2, x), zip(K, L))) if len(K)==len(L) else 0
dotprod([1, 2, 3, 4], [5, 6, 7, 8])
Out[43]: 70
dotprod([1, 2, 3, 4], [5, 6, 7])
Out[44]: 0
回答by abhikarma1493
Using list comprehension, given V1 and V2 are two vectors(lists):
使用列表理解,给定 V1 和 V2 是两个向量(列表):
sum([x*y for x,y in zip(V1,V2)])
回答by Harsh M Pandya
vector_a = [1., 2., 3.]
vector_b = [2., 2., 2.]
z=list(zip(vector_a,vector_b))
a=sum([x*y for x,y in zip(vector_a,vector_b)])
for m,n in z:
print(str(m)+"*",str(n))
print(a)
#hope you got it `
Using Zip function provides lesser complexity making it more efficient to use in case of dot products or any multiple list operations.
使用 Zip 函数提供了较低的复杂性,使其在点积或任何多个列表操作的情况下更有效地使用。