Storing the values of multiple functions in “for _ in range(100):“

Question:

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.

Asked By: Magnus Moller

||

Answers:

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)
Answered By: Thatcher Thornberry
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.