Problem definition:
- I have a dictionary with
N > 0
parameter lists as values and parameter names as keys. - Each of the lists may be of a different length (e.g.,
k > 0, l > 0, m > 0...
). - My goal is to produce a unified list of tuples
[(a11, a21, a31, ...), (a12, a22, a32, ...), ..., (a1k, a2l, a3m, ...)]
, where each tuple is a set of lengthN
, and containing corresponding parameter values.
Example:
Given the following parameter dictionary:
const_params_dict= dict(a=[-1, 1], b=[-5, -4, -3, -2, -1, 1, 2, 3, 4, 5], th=[0.5])
the desired output will be:
Out: [(-1, -5, 0.5), (-1, -4, 0.5), (-1, -3, 0.5), (-1, -2, 0.5), ..., (-1, 5, 0.5),
(1, -5, 0.5), (1, -4, 0.5), (1, -3, 0.5), (1, -3, 0.5) , ..., (1, 5, 0.5)]
Solution for a known value of lists (i.e. known N
):
I can think of a way to solve this problem for a known value of N
, as shown in Code 1
Code 1:
[(a, b, th) for a in const_params_dict['a'] for b in const_params_dict['b'] for th in const_params_dict['th']]
which results in:
[(-1, -5, 0.5),
(-1, -4, 0.5),
(-1, -3, 0.5),
(-1, -2, 0.5),
(-1, -1, 0.5),
(-1, 1, 0.5),
(-1, 2, 0.5),
(-1, 3, 0.5),
(-1, 4, 0.5),
(-1, 5, 0.5),
(1, -5, 0.5),
(1, -4, 0.5),
(1, -3, 0.5),
(1, -2, 0.5),
(1, -1, 0.5),
(1, 1, 0.5),
(1, 2, 0.5),
(1, 3, 0.5),
(1, 4, 0.5),
(1, 5, 0.5)]
My question:
How can I generalize this solution for an arbitrary value of N
in the most elegant and efficient way?