分解字典 - 获取字典中值的所有组合

Explode a dict - Get all combinations of the values in a dictionary

我想将字典中值的所有组合作为多个字典(每个字典包含原始值的每个键,但只包含原始值的一个值)。假设我想参数化一个函数调用:

kwargs = {'a': [1, 2, 3], 'b': [1, 2, 3]}

如何获得所有组合的列表,如下所示:

combinations = [{'a': 1, 'b': 1}, {'a': 1, 'b': 2}, {'a': 1, 'b': 3},
                {'a': 2, 'b': 1}, {'a': 2, 'b': 2}, {'a': 2, 'b': 3},
                {'a': 3, 'b': 1}, {'a': 3, 'b': 2}, {'a': 3, 'b': 3}]

原始kwargs中可以有任意数量的键,每个值保证是可迭代的,但值的数量不固定。

如果可能:最后的 combinations 应该是生成器(不是列表)。

您可以将 kwargs 展平成这样

>>> kwargs = {'a': [1, 2, 3], 'b': [1, 2, 3]}
>>> flat = [[(k, v) for v in vs] for k, vs in kwargs.items()]
>>> flat
[[('b', 1), ('b', 2), ('b', 3)], [('a', 1), ('a', 2), ('a', 3)]]

然后,你可以这样使用itertools.product

>>> from itertools import product
>>> [dict(items) for items in product(*flat)]
[{'a': 1, 'b': 1},
 {'a': 2, 'b': 1},
 {'a': 3, 'b': 1},
 {'a': 1, 'b': 2},
 {'a': 2, 'b': 2},
 {'a': 3, 'b': 2},
 {'a': 1, 'b': 3},
 {'a': 2, 'b': 3},
 {'a': 3, 'b': 3}]

itertools.product 实际上 returns 一个迭代器。所以你可以按需获取值并构建你的字典。或者您可以使用 map,它也是 returns 一个迭代器。

>>> for item in map(dict, product(*flat)):
...     print(item)
...
...
{'b': 1, 'a': 1}
{'b': 1, 'a': 2}
{'b': 1, 'a': 3}
{'b': 2, 'a': 1}
{'b': 2, 'a': 2}
{'b': 2, 'a': 3}
{'b': 3, 'a': 1}
{'b': 3, 'a': 2}
{'b': 3, 'a': 3}

只是另一种方式,先构建值元组,然后再与键组合(与@thefourtheye 的方式完全相反:-)。

>>> combinations = (dict(zip(kwargs, vs)) for vs in product(*kwargs.values()))
>>> for c in combinations:
        print(c)

{'a': 1, 'b': 1}
{'a': 1, 'b': 2}
{'a': 1, 'b': 3}
{'a': 2, 'b': 1}
{'a': 2, 'b': 2}
{'a': 2, 'b': 3}
{'a': 3, 'b': 1}
{'a': 3, 'b': 2}
{'a': 3, 'b': 3}