Cartesian product of a dictionary of lists

前端 未结 4 1932
有刺的猬
有刺的猬 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:41

    By the way, this is not a permutation. A permutation is a rearrangement of a list. This is an enumeration of possible selections from lists.

    Edit: after remembering that it was called a Cartesian product, I came up with this:

    import itertools
    options = {"number": [1,2,3], "color": ["orange","blue"] }
    product = [x for x in apply(itertools.product, options.values())]
    print [dict(zip(options.keys(), p)) for p in product]
    

提交回复
热议问题