我想编写一个函数来计算两个向量的点积,而无需使用额外的库。以下是我所做的尝试。有人可以指出我代码中的缺陷吗?
def dot(vector01,vector02):
result= []
for i in range(len(vector01), len(vector02)):
total = 0
total += vector01[i] * vector02[i]
result.append(total)
return result
if len(vector01) == len(vector02):
return result
else:
print(error)
vector01 = [2, 3, 4]
vector02 = [4, 2, 1]
print(dot(vector01,vector02))
答案 0 :(得分:1)
def dot(v1,v2):
if len(v1) != len(v2):
raise ValueError
return sum(i1*i2 for i1,i2 in zip(v1,v2))