define a Python function def construct_grid(params). The input params will be a dictionary mapping strings to arrays
Question:
define a Python function def construct_grid(params). The input params will be a dictionary mapping strings to arrays (or array-like data structures such as lists). Consider each string a parameter name and each value in the corresponding array to be a candidate value for that parameter.
The function should return a list of dictionaries which satisfies the following:
1. Each dictionary maps each parameter name to exactly one candidate value for that parameter.
2. Exactly one dictionary exists for each combination of parameters.
3. The list is sorted by the values associated with each key. The sort priority for each key should be based on their lexographical order.
For example, construct_grid({'b': [1, 2], 'z': [3, 5, 6], 'a': [100, 10]}) should return as output:
[{'b': 1, 'z': 3, 'a': 10},
{'b': 1, 'z': 5, 'a': 10},
{'b': 1, 'z': 6, 'a': 10},
{'b': 2, 'z': 3, 'a': 10},
{'b': 2, 'z': 5, 'a': 10},
{'b': 2, 'z': 6, 'a': 10},
{'b': 1, 'z': 3, 'a': 100},
{'b': 1, 'z': 5, 'a': 100},
{'b': 1, 'z': 6, 'a': 100},
{'b': 2, 'z': 3, 'a': 100},
{'b': 2, 'z': 5, 'a': 100},
{'b': 2, 'z': 6, 'a': 100}]
Notice the following:
In this example, there are 3 parameters. The length of the lists assigned to the parameters are 2, 3, and 2.
Thus there are 12 (2*3*2)possible combinations.
Each possible combination is represented exactly once in the list.
The list is sorted first by the 'a' value, then by the 'b' value, and finally by the 'z' value.
Keep in mind that the function needs to work for an arbitrary dictionary (the number of keys, the keys themselves, and the values can be anything which meets the which has the structure given earlier in the prompt).
Note: the functions itertools.product or numpy.meshgrid might be helpful in solving this problem.
Building Java Programs A Back To Basics Approach
ISBN: 9780135471944
5th Edition
Authors: Stuart Reges, Marty Stepp