David542
David542

Reputation: 110267

Matrix scalar multiplication

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]

Upvotes: 5

Views: 62157

Answers (2)

levi
levi

Reputation: 22697

Another approach

map(lambda x:x*1.2738,list_of_items)

Upvotes: 2

Nitish
Nitish

Reputation: 7166

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.

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

Upvotes: 18

Related Questions