python - Cartesian product of a dictionary of lists -
i'm trying write code test out cartesian product of bunch of input parameters.
i've looked @ itertools
, product
function not want. there simple obvious way take dictionary arbitrary number of keys and arbitrary number of elements in each value, , yield dictionary next permutation?
input:
options = {"number": [1,2,3], "color": ["orange","blue"] } print list( my_product(options) )
example output:
[ {"number": 1, "color": "orange"}, {"number": 1, "color": "blue"}, {"number": 2, "color": "orange"}, {"number": 2, "color": "blue"}, {"number": 3, "color": "orange"}, {"number": 3, "color": "blue"} ]
ok, @dfan telling me looking in wrong place. i've got now:
def my_product(dicts): return (dict(izip(dicts, x)) x in product(*dicts.itervalues()))
Comments
Post a Comment