I ran several times an application with different input parameters in order to collect execution times.
The input parameters are 6: v, n, m, b, p and c.
Conceptually I can think of my results as a multidimensional array, where any dimension is a different parameter: times[A][B][C][D][E][F] would contain the execution time of the simulation using parameters v=A, n=B, m=C, b=D, p=E and c=F.
I’d like to be able to fix some of these parameters and iterate over the others:
for A:
for C:
for F:
times[A][0][C][0][0][F]
The input parameters values are sparse, so I should use dictionaries instead of lists.
I was thinking about using a dict of dict of dict of dict of dict of dict to do the whole thing, each execution time would be represented like this:
times = { A:{ B:{ C:{ D:{ E:{ F:{time} } } } } } }
but this solution doesn’t look elegant at all: building the whole structure and iterating over it is a pain.
Is there any better way to work with my data?
First, if you have to use a dictionary, why not just create a single dictionary, using tuples to index it? Second, use
itertools.productto avoid troublesome nested loops:However, there might be better ways to create a sparse array.
scipyprovides sparse matrices, but they are 2-d only, I believe.Here are some other usage patterns you might find useful:
To be less oblique, here’s how you would hold one variable constant: just pass a one-item sequence to
itertools.product: