Cartesian product of a dictionary of lists

前端 未结 4 1921
有刺的猬
有刺的猬 2020-11-27 12:08

I\'m trying to write some code to test out the Cartesian product of a bunch of input parameters.

I\'ve looked at itertools, but its product

4条回答
  •  自闭症患者
    2020-11-27 12:15

    Ok, thanks @dfan for telling me I was looking in the wrong place. I've got it now:

    from itertools import product
    def my_product(inp):
        return (dict(zip(inp.keys(), values)) for values in product(*inp.values())
    

    EDIT: after years more Python experience, I think a better solution is to accept kwargs rather than a dictionary of inputs; the call style is more analogous to that of the original itertools.product. Also I think writing a generator function, rather than a function that returns a generator expression, makes the code clearer. So:

    def product_dict(**kwargs):
        keys = kwargs.keys()
        vals = kwargs.values()
        for instance in itertools.product(*vals):
            yield dict(zip(keys, instance))
    

    and if you need to pass in a dict, list(product_dict(**mydict)). The one notable change using kwargs rather than an arbitrary input class is that it prevents the keys/values from being ordered, at least until Python 3.6.

提交回复
热议问题