如何将列表中的各个元素与数字相乘?

时间:2021-05-10 13:43:35
S=[22, 33, 45.6, 21.6, 51.8]
P=2.45

Here S is an array

这里S是一个数组

How will I multiply this and get the value?

我将如何乘以这个并获得价值?

SP=[53.9, 80.85, 111.72, 52.92, 126.91]

3 个解决方案

#1


32  

You can use built-in map function:

您可以使用内置的地图功能:

result = map(lambda x: x * P, S)

or list comprehensions that is a bit more pythonic:

或列出更加pythonic的理解:

result = [x * P for x in S]

#2


53  

In numpy it is quite simple

在numpy中它很简单

import numpy as np
P=2.45
S=[22, 33, 45.6, 21.6, 51.8]
SP = P*np.array(S)

I recommend taking a look at the numpy tutorial for an explanation of the full capabilities of numpy's arrays:

我建议看一下numpy教程,了解numpy数组的全部功能:

http://www.scipy.org/Tentative_NumPy_Tutorial

http://www.scipy.org/Tentative_NumPy_Tutorial

#3


15  

If you use numpy.multiply

如果你使用numpy.multiply

S = [22, 33, 45.6, 21.6, 51.8]
P = 2.45
multiply(S, P)

It gives you as a result

它为您提供了结果

array([53.9 , 80.85, 111.72, 52.92, 126.91])

#1


32  

You can use built-in map function:

您可以使用内置的地图功能:

result = map(lambda x: x * P, S)

or list comprehensions that is a bit more pythonic:

或列出更加pythonic的理解:

result = [x * P for x in S]

#2


53  

In numpy it is quite simple

在numpy中它很简单

import numpy as np
P=2.45
S=[22, 33, 45.6, 21.6, 51.8]
SP = P*np.array(S)

I recommend taking a look at the numpy tutorial for an explanation of the full capabilities of numpy's arrays:

我建议看一下numpy教程,了解numpy数组的全部功能:

http://www.scipy.org/Tentative_NumPy_Tutorial

http://www.scipy.org/Tentative_NumPy_Tutorial

#3


15  

If you use numpy.multiply

如果你使用numpy.multiply

S = [22, 33, 45.6, 21.6, 51.8]
P = 2.45
multiply(S, P)

It gives you as a result

它为您提供了结果

array([53.9 , 80.85, 111.72, 52.92, 126.91])