Python 矩阵标量乘法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28821503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:47:55  来源:igfitidea点击:

Matrix scalar multiplication

python

提问by David542

Is there a more 'mathematical' way to do the following:

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

1.2738 * (list_of_items)

So for what I'm doing is:

所以我正在做的是:

[1.2738 * item for item in list_of_items]

采纳答案by Nitish

The mathematical equivalent of what you're describing is the operation of multiplication by a scalar for a vector. Thus, my suggestion would be to convert your list of elements into a "vector" and then multiply that by the scalar.

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

A standard way of doing that would be using numpy.

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

Instead of

代替

1.2738 * (list_of_items)

You can use

您可以使用

import numpy
1.2738 * numpy.array(list_of_items)

Sample Output:

示例输出:

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 ])

回答by levi

Another approach

另一种方法

map(lambda x:x*1.2738,list_of_items)