【发布时间】:2021-08-11 03:53:17
【问题描述】:
我已经开始了数据科学课程,第一个作业中的一个问题是编写一个函数,该函数返回两个向量的加法、乘法和点积,格式如下:
def list_mul(u, v):
"""
Given two vectors, calculate and return the following quantities:
- element-wise sum
- element-wise product
- dot product
If the two vectors have different dimensions,
you should raise a ValueError
:param u: first vector (list)
:param v: second vector (list)
:return: the three quantities above
:rtype: list, list, float
:raise ValueError:
"""
如果我可以使用 numpy,我会发现这真的很容易,但要求是仅使用 vanilla python 编写函数 - 我们可以使用 import math 之类的东西,但不能使用自定义包。
如果有人能够提供帮助或提供一些建议,我将不胜感激?
非常感谢,
安德鲁
【问题讨论】:
-
导入算子后:sum = map(operator.add, u, v) product = map(operator.mul, u, v) dot_product = sum(map(operator.mul, u, v) )