有道python题不会 求解答 两个n 向量的内积(也称作点积)定义为如下的标量:a b=alb1+a2b2+...tanbn即对应元索乘积的和
代码与效效果截图如下: 如有帮助给个采纳谢谢
def dot_product(a, b):
if len(a) != len(b):
return "Error: the lengths of the vectors do not match."
result = 0
for i in range(len(a)):
result += a[i] * b[i]
return result
# 函数测试
a = [1, 2, 3]
b = [4, 5, 6]
print(dot_product(a, b))
这题出的前后矛盾,前面说内积是一个数
后面要用元组或列表返回
很显然内积不是一个数而是一个向量
def inner_product(a,b):
return [x[0]*x[1] for x in zip(a,b)]