Skip to content Skip to sidebar Skip to footer

Overwriting An Array In Numpy Function Python

I am trying to write a numpy function that iterates with itself to update the values of its function. If for example Random_numb was equal to [50, 74, 5, 69, 50]. So the calculatio

Solution 1:

IIUC you're looking for prod:

import numpy as np

Starting_val = 10
Random_numb = np.array([50, 74, 5, 69, 50])

Random_numb.prod(initial=Starting_val)
#638250000

If you're interested in the multiplied values of the array it'll be cumprod:

Starting_val * Random_numb.cumprod()
# array([      500,     37000,    185000,  12765000, 638250000])

Solution 2:

Here you are just multiplying the Starting_val with your array of random numbers. You are not updating Starting_val each time. Try out the below code

for i in range(len(Random_numb)):
    Random_numb[i] *= Starting_val
    Starting_val = Random_numb[i]

Hope this solves your query!


Post a Comment for "Overwriting An Array In Numpy Function Python"