gpt4 book ai didi

python - 矩阵标量乘法

转载 作者:太空狗 更新时间:2023-10-30 01:41:31 26 4
gpt4 key购买 nike

是否有更“数学”的方法来执行以下操作:

1.2738 * (list_of_items)

所以我正在做的是:

[1.2738 * item for item in list_of_items]

最佳答案

您所描述的数学等价物是向量乘以标量的运算。因此,我的建议是将您的元素列表转换为“向量”,然后将其乘以标量。

这样做的标准方法是使用 numpy

代替

1.2738 * (list_of_items)

你可以使用

import numpy
1.2738 * numpy.array(list_of_items)

示例输出:

In [8]: list_of_items
Out[8]: [1, 2, 4, 5]

In [9]: import numpy

In [10]: 1.2738 * numpy.array(list_of_items)
Out[10]: array([ 1.2738, 2.5476, 5.0952, 6.369 ])

关于python - 矩阵标量乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821503/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com