简体   繁体   中英

Storing Arrays when looping through thousands of parameters

I need to loop through every combination of 6 arrays of size 5 (5^6 combinations). After each loop I need to store a function output along with what each of those numbers were. For example:

def function1(a, b, c, d, e, f): 

    return output

for A,B,C,D,E,F in [(A,B,C,D,E,F) for A in a for B in b for C in c for 
                    D in d for E in e for F in f]:
    function1(A,B,C,D,E,F)

In the above example for each iteration I would need to store the output of "function1" along with all the parameters that went into function1 aj. What is the most pythonic way to organize this? I was going to do this:

 all_results = np.zeros(5^6,7)

for ....
    answer = function1(a,b,c,d,e,f)
    all_results[i,:] = [answer, a,b,c,d,e,f]

But thought that it might be slow and archaic. Is there a better way to do this.

import itertools.product

all_results = {}
for p in itertools.product(a,b,c,d,e,f):
   all_results[p] = function1(*p)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM