Dot Product in Python without NumPy

别来无恙 提交于 2019-12-06 03:44:18

问题


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?

For example:

V_1=[1,2,3]
V_2=[4,5,6]

Dot(V_1,V_2)

Answer: 32


回答1:


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


来源:https://stackoverflow.com/questions/35208160/dot-product-in-python-without-numpy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!