Home > Enterprise >  Storing the values of multiple functions in ``for _ in range(100):``
Storing the values of multiple functions in ``for _ in range(100):``

Time:10-15

I am using numpy. I want to store values from different functions for _ in range(100): that is inside the previous loop.

The original code that I want to implement would be equivalent to the following for the purpose of this question

for _ in range(100):
    x=npr.normal(0,2,(1,100)) 
    5x
    9x

How would I store the values of 2 different functions f(x)=5x and g(x)=9x, in two separate lists or arrays?

In the following answer(How do I call a function twice or more times consecutively?) a method is given, but only for one function([do() for _ in range(3)]), not a sequence of two.

How should I store the values?

Thanks in advance.

CodePudding user response:

Initialize an array before running the loop and then append the result value to the array.

func_1_results = []
func_2_results = []
for _ in range(100):
    x=npr.normal(0,2,(1,100)) 
    func_1_results.append(5 * x)
    func_2_results.append(9 * x)
  • Related