0

I'm quite new to Python and I am looking for a datastructure to store results from measurements with different parameters.

I have two parameters, param_1_vals = [10, 20, 30, 40] and param_2_vals = [1, 2, 3, 4] with which I do some caluculations and finally obtain a result result, which is a Numpy array. Since I loop through the parameters in nested for oops, for each combination of parameters I get a different result array.

I wonder if there is a multidimensional datastructure that is able to store all the results for all parameter combinations can be indexed like

 result_(param_1,param_2) 

which gives me the corresponding Numpy array for that specific combination of parameters. Preferably, the parameters can not only be numeric but also strings.

1
  • 1
    take a look at pandas Commented Jan 7, 2016 at 10:14

2 Answers 2

1

For small input arrays, the simplest solution is probably to use a 2D array with row indexes from the first input array and column indexes from the second input array:

result = numpy.empty((len(param_1_vals),len(param_2_vals)))
result[i,j] = yourCalculation(param_1_vals[i],param_2_vals[j])

It is more elegant (and more efficient for large datasets) to store your results in a dictionary with a tuple of input values as keys:

result = {}
result[(a,b)] = yourCalculation(a,b)

You can also use the indexes as dictionary keys if a and b are floats or mutable variables.

result = {}
result[(i,j)] = yourCalculation(param_1_vals[i],param_2_vals[j])

In general, what you're trying to achieve is memoization.

Sign up to request clarification or add additional context in comments.

Comments

1

Use dict, a key-value datastructure

result_ = {}

foreach combination param_1,param_2
form a tuple t = (param_1,param_2) and use it as key

result_[t]= result

1 Comment

Isn't this the same as the more elegant solution @leeladam suggested?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.