How to multiply individual elements of a list with a number?
Asked Answered
U

4

57
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]
Uncurl answered 19/11, 2011 at 15:19 Comment(1)
In Python S is not an array, it is a list. There is a very big difference betweeb the two types of containers. If you want numerical arrays, use numpy.Cullum
V
42

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]
Vesuvianite answered 19/11, 2011 at 15:22 Comment(2)
Just as a note, operations on arrays, like scalar multiplication are highly optimized in numpy, and are significantly faster than list comprehensions. It is generally advisable to not treat numpy arrays like python lists.Delinquent
"It is generally advisable to not treat numpy arrays like python lists", what does that mean in general? and how is it related to the current answer which doesn't use NumPy? Is this answer still true in 2021 (probably but still not sure, maybe some new operator has appeared to indicate we really want the distributive operation and not the list replication)?Rinse
D
77

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:

https://scipy.github.io/old-wiki/pages/Tentative_NumPy_Tutorial

Delinquent answered 19/11, 2011 at 15:25 Comment(0)
V
42

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]
Vesuvianite answered 19/11, 2011 at 15:22 Comment(2)
Just as a note, operations on arrays, like scalar multiplication are highly optimized in numpy, and are significantly faster than list comprehensions. It is generally advisable to not treat numpy arrays like python lists.Delinquent
"It is generally advisable to not treat numpy arrays like python lists", what does that mean in general? and how is it related to the current answer which doesn't use NumPy? Is this answer still true in 2021 (probably but still not sure, maybe some new operator has appeared to indicate we really want the distributive operation and not the list replication)?Rinse
F
21

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])
Frustule answered 14/5, 2013 at 17:50 Comment(0)
M
1

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]
Monometallic answered 3/4, 2019 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.