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]
We can use numpy. prod() from import numpy to get the multiplication of all the numbers in the list. It returns an integer or a float value depending on the multiplication result.
Lists and strings have a lot in common. They are both sequences and, like pythons, they get longer as you feed them. Like a string, we can concatenate and multiply a Python list.
You can use any scalar types (int, float, Boolean, etc.) and any method other than traversal with the list multiplication function in python language. The first illustration was all about using a single list; however, we have used two lists in our second illustration.
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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With