此问题已经在这里有了答案:
𝑣⋅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]
为什么不随便拿走你所拥有的和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)