S = [22, 33, 45.6, 21.6, 51.8]
P = 2.45
Here S
is an array
How will I multiply this and get the value?
SP = [53.9, 80.85, 111.72, 52.92, 126.91]
Georgy
10.7k7 gold badges62 silver badges68 bronze badges
asked Nov 19, 2011 at 15:19
1
In NumPy it is quite simple
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:
//scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial
Georgy
10.7k7 gold badges62 silver badges68 bronze badges
answered Nov 19, 2011 at 15:25
JoshAdelJoshAdel
63.7k24 gold badges138 silver badges136 bronze badges
0
You can use built-in map
function:
result = map[lambda x: x * P, S]
or list comprehensions that is a bit more pythonic:
result = [x * P for x in S]
answered Nov 19, 2011 at 15:22
KL-7KL-7
44k9 gold badges86 silver badges74 bronze badges
2
If you use 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]]
nbro
14.3k27 gold badges103 silver badges188 bronze badges
answered May 14, 2013 at 17:50
DKKDKK
1,8404 gold badges15 silver badges20 bronze badges
Here is a functional approach using
map
, itertools.repeat
and operator.mul
:
import operator
from itertools import repeat
def scalar_multiplication[vector, scalar]:
yield from map[operator.mul, vector, repeat[scalar]]
Example of usage:
>>> v = [1, 2, 3, 4]
>>> c = 3
>>> list[scalar_multiplication[v, c]]
[3, 6, 9, 12]
answered Apr 3, 2019 at 9:34
GeorgyGeorgy
10.7k7 gold badges62 silver badges68 bronze badges
Posted on: March 12, 2021 by Deven
In this article, you will learn how to multiply array by scalar in python.
Let’s say you have 2 arrays that need to be multiplied by scalar n
.
array1 = np.array[[1, 2, 3]]
array2 = np.array[[[1, 2], [3, 4]]]
n = 5
Numpy multiply array by scalar
In order to multiply array by scalar in
python, you can use np.multiply[]
method.
import numpy as np
array1 = np.array[[1, 2, 3]]
array2 = np.array[[[1, 2], [3, 4]]]
n = 5
np.multiply[array1,n]
np.multiply[array2,n]
Share on social media
//
PreviousNext