没有NumPy的Python点积 [英] Dot Product in Python without NumPy
本文介绍了没有NumPy的Python点积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种方法可以在不使用NumPy或Python中的Operation模块的情况下,对包含值的两个列表进行点积运算?这样代码就尽可能地简单?
Is there a way that you can preform a dot product of two lists that contain values without using NumPy or the Operation module in Python? So that the code is as simple as it could get?
例如:
V_1=[1,2,3]
V_2=[4,5,6]
Dot(V_1,V_2)
答案:32
推荐答案
没有numpy,您可以为使用zip
和sum
的点积编写函数.
Without numpy, you can write yourself a function for the dot product which uses zip
and sum
.
>>> def dot(v1, v2):
... return sum(x*y for x,y in zip(v1,v2))
...
>>> dot([1,2,3], [4,5,6])
32
这篇关于没有NumPy的Python点积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文