-1

I've defined a function that outputs two arrays [x] and [y]. My goal is to then call that function a number of times (in this example 10) and store each of those outputs into a different array so that I can average each array for x and y. I've started my code to call the function 10 times as follows: def myfunction() = myexamplefunction; in this function I've defined my output arrays x and y and can show that my function populates these values. For example, the x array looks like [[0, 1, 2, 1, 2, 3, 4, 5, 6, 7, 6, 7, 8, 9, 10] as an ouput.

Xtotal=[0] #new array that stores all the x values
Ytotal=[0] #new array that stores all the y values
for i in myfunction(range(10)): 
    Xtotal.append(x) #append the whole x array
    Ytotal.append(y) #append the whole y array

alternately I've tried:

for i in range(10):
    myfunction()
    Xtotal.append(x) #append the whole x array
    Ytotal.append(y) #append the whole y array

I've succeeded in calling my function 10 times but cannot get each output to nest in my new arrays. Any help would be appreciated. I'm learning python for this specific project so yes, I'm sure I'm missing some very basic things.

Skruberk
  • 199
  • 2
  • 8
  • 1
    Not really related, but I noticed the code `Xtotal=[0]`. This initializes a new list with one item in it, `0`. Did you mean to just create a new empty list, like `Xtotal = []`? – Random Davis Jun 14 '22 at 19:18
  • 1
    How do "myfunction" and "function" look like? Show a sample of real and desired output (can be shortened). – Michael Butscher Jun 14 '22 at 19:21

2 Answers2

1

You must be sure that your function is returning the arrays at first e.g.:

def myfunction():
    *
    *
    return X, Y             # <-- this line

then you must call this function and puts its results in new variables i.e.:

Xtotal = []
Ytotal = []
for i in range(10):
    x, y = myfunction()        # <-- this line: put returned arrays X, Y in x, y
    Xtotal.append(x) #append the whole x array
    Ytotal.append(y) #append the whole y array
Ali_Sh
  • 2,667
  • 3
  • 43
  • 66
0

A basic nested loop

alist =[]
for i in range(3):
    blist = []
    for j in range(4):
        blist.append(func(i,j))
    alist.append(blist)

or

alist = [func(i,j) for j in range(4)] for i in range(3)]

Note that my func takes two values (scalar i and j), and returns something, such as a number. The result will be a list of lists of numbers.

You describe myfunction as returning 2 arrays. But don't say anything about its arguments (inputs) if any.

In [12]: def myfunction():
    ...:     return np.arange(3), np.ones((4))
    ...:     
In [15]: alist = []
    ...: for i in range(3):
    ...:     alist.append(myfunction())
    ...:     

In [16]: alist
Out[16]: 
[(array([0, 1, 2]), array([1., 1., 1., 1.])),
 (array([0, 1, 2]), array([1., 1., 1., 1.])),
 (array([0, 1, 2]), array([1., 1., 1., 1.]))]

That's a list of tuples, each of which contains the 2 arrays produced by the function. What is right - or wrong - about that result?

another way of collecting results from many calls:

In [17]: alist = []; blist = []
    ...: for i in range(3):
    ...:     x,y = myfunction()
    ...:     alist.append(x); blist.append(y)
    ...:     

In [18]: alist
Out[18]: [array([0, 1, 2]), array([0, 1, 2]), array([0, 1, 2])]

In [19]: blist
Out[19]: [array([1., 1., 1., 1.]), array([1., 1., 1., 1.]), array([1., 1., 1., 1.])]
hpaulj
  • 221,503
  • 14
  • 230
  • 353