[英]How to generate all combination from values in dict of lists in Python
I would like to generate all combinations of values which are in lists indexed in a dict:我想生成在字典中索引的列表中的所有值组合:
{'A':['D','E'],'B':['F','G','H'],'C':['I','J']}
Each time, one item of each dict entry would be picked and combined to items from other keys, so I have:每次,每个 dict 条目的一个项目将被挑选并与其他键的项目组合,所以我有:
['D','F','I']
['D','F','J']
['D','G','I']
['D','G','J']
['D','H','I']
...
['E','H','J']
I know there is a something to generate combinations of items in list in itertools
but I don't think I can use it here since I have different "pools" of values.我知道有一些东西可以在
itertools
生成列表中的项目组合,但我认为我不能在这里使用它,因为我有不同的“池”值。
Is there any existing solution to do this, or how should I proceed to do it myself, I am quite stuck with this nested structure.是否有任何现有的解决方案可以做到这一点,或者我应该如何自己进行,我对这个嵌套结构非常坚持。
import itertools as it
my_dict={'A':['D','E'],'B':['F','G','H'],'C':['I','J']}
allNames = sorted(my_dict)
combinations = it.product(*(my_dict[Name] for Name in allNames))
print(list(combinations))
Which prints:哪个打印:
[('D', 'F', 'I'), ('D', 'F', 'J'), ('D', 'G', 'I'), ('D', 'G', 'J'), ('D', 'H', 'I'), ('D', 'H', 'J'), ('E', 'F', 'I'), ('E', 'F', 'J'), ('E', 'G', 'I'), ('E', 'G', 'J'), ('E', 'H', 'I'), ('E', 'H', 'J')]
If you want to keep the key:value
in the permutations you can use:如果您想保留
key:value
的排列,您可以使用:
import itertools
keys, values = zip(*my_dict.items())
permutations_dicts = [dict(zip(keys, v)) for v in itertools.product(*values)]
this will provide you a list of dicts with the permutations:这将为您提供具有排列的字典列表:
print(permutations_dicts)
[{'A':'D', 'B':'F', 'C':'I'},
{'A':'D', 'B':'F', 'C':'J'},
...
]
disclaimer
not exactly what the OP was asking, but google send me here looking for that. disclaimer
不完全是 OP 的要求,但谷歌将我发送到这里寻找。
How about using ParameterGrid from scikit-learn?如何使用 scikit-learn 中的 ParameterGrid? It creates a generator over which you can iterate in a normal for loop.
它创建了一个生成器,您可以在正常的 for 循环中对其进行迭代。 In each iteration, you will have a dictionary containing the current parameter combination.
在每次迭代中,您将拥有一个包含当前参数组合的字典。
from sklearn.model_selection import ParameterGrid
params = {'A':['D','E'],'B':['F','G','H'],'C':['I','J']}
param_grid = ParameterGrid(params)
for dict_ in param_grid:
# Do something with the current parameter combination in ``dict_``
print(dict_["A"])
print(dict_["B"])
print(dict_["C"])
As a complement, here is code that does it Python so that you get the idea, but itertools
is indeed more efficient.作为补充,这里是用 Python 执行的代码,以便您了解这个想法,但
itertools
确实更有效。
res = [[]]
for _, vals in my_dict.items():
res = [x+[y] for x in res for y in vals]
print(res)
from itertools import combinations
a=['I1','I2','I3','I4','I5']
list(combinations(a,2))
The output will be:输出将是:
[('I1', 'I2'),
('I1', 'I3'),
('I1', 'I4'),
('I1', 'I5'),
('I2', 'I3'),
('I2', 'I4'),
('I2', 'I5'),
('I3', 'I4'),
('I3', 'I5'),
('I4', 'I5')]
除了使用Maryam提供的笛卡尔积之外,您还可以使用朴素的嵌套循环来获得您想要的效果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.