将2个向量(列表)相乘以得到总输出[重复项]

问题描述 投票:-1回答:1

此问题已经在这里有了答案:

𝑣⋅w = ‖𝑣⎯‖‖w‖cos𝜃

定义一个函数,使其接受v和w作为列表并吐出其内积。



def inner_product(list1, list2):
    a = list1
    for i in range(0, len(a)):

        b = list2
        ab = []

        s = ab.append(a[i]*b[i])

    return s

这一直吐出'none'。

没有功能,它吐出[1,10]。

但是我需要这样:

inner_product([1,2],[1,5])

给我11,而不是[1,10]

python vector product multiplication dot
1个回答
-3
投票

为什么不随便拿走你所拥有的和return sum(s)

编辑:清理代码:

def inner_product(list1, list2):
  ab = []
  for i in range(0, len(list1)):
    ab.append(list1[i]*list2[i])
  return sum(ab)
© www.soinside.com 2019 - 2024. All rights reserved.