bharath bharath - 17 days ago 10
Python Question

In Python how will you multiply individual elements of a list with a floating point or integer number?

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]

Answer

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