我的字典格式如下:
{
'variable_1': [a1, b1, c1],
'variable_2': [a2, b2, c2],
'variable_3': [a3, b3, c3],
...
}
(此词典中有n
个条目)
我有一个问题想要获取所有不同值的可能组合,例如:
[(a1, a2, a3), (b1, a2, a3), (c1, a2, a3), (a1, b2, a3), ...]
我知道这可以通过使用带有itertools
的笛卡尔积来完成。但是,我注意到如果结果采用以下形式,我的问题会容易得多:
[{'variable1': a1, 'variable2':a2, 'variable3': a3}, {'variable1': b1, 'variable2':a2, 'variable3': a3}, ...]
有一种简单的方法吗?
答案 0 :(得分:0)
import itertools
d = {
'variable_1': ['a1', 'b1', 'c1'],
'variable_2': ['a2', 'b2', 'c2'],
'variable_3': ['a3', 'b3', 'c3'],
}
keys = (list(d.keys())) # ['variable_1', 'variable_2', 'variable_3']
values = (list(itertools.product(*(list(d.values()))))) #[('a1', 'a2', 'a3'), ('a1', 'a2', 'b3'), ('a1', 'a2', 'c3')...]
dictionary = []
for val in values:
dictionary.append(dict(zip(keys, val)))
输出:
[{'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'a3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'b3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'c3'}, {'variable_1': 'a1', 'variable_2': 'b2', 'variable_3': 'a3'},...]
示例:
d = {
'variable_1': ['a1', 'b1', 'c1','d1'],
'variable_2': ['a2', 'b2', 'c2','d2'],
'variable_3': ['a3', 'b3', 'c3','d3'],
'variable_4': ['a3', 'b3', 'c3','d4'],
}
输出:
[{'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'a3', 'variable_4': 'a3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'a3', 'variable_4': 'b3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'a3', 'variable_4': 'c3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'a3', 'variable_4': 'd4'}, ...]
答案 1 :(得分:0)
您仍然需要itertools.product
:
a = {'v1': [1,2], 'v2': [3,4]}
gen = (dict(zip(a.keys(), x)) for x in itertools.product(*a.values()))
>>> list(gen)
[{'v1': 1, 'v2': 3}, {'v1': 1, 'v2': 4}, {'v1': 2, 'v2': 3}, {'v1': 2, 'v2': 4}]
您可以按从右到左的方式阅读生成器:
*
被称为the splat operator):>>> list(itertools.product(*a.values()))
[(1, 3), (1, 4), (2, 3), (2, 4)]
zip
对密钥和乘积结果进行配对。>>> zip(['v1', 'v2'], [1, 3])
[('v1', 1), ('v2', 3)]
>>> dict([('v1', 1), ('v2', 3)])
{'v1': 1, 'v2': 3}
答案 2 :(得分:0)
正如阿兰·费伊(Aran-Fey)在评论中所说,您可以将元组从itertools.product
变成字典:
import itertools
d = {
'variable_1': [1, 2, 3],
'variable_2': [4, 5, 6],
'variable_3': [7, 8, 9],
}
all_elements = [[(key, element) for element in d[key]] for key in d]
print("All elements: {}".format(all_elements))
product = itertools.product(*all_elements)
print("As a dict:")
for i in product:
print(dict(i))
这将产生以下结果:
All elements: [
[('variable_1', 1), ('variable_1', 2), ('variable_1', 3)],
[('variable_2', 4), ('variable_2', 5), ('variable_2', 6)],
[('variable_3', 7), ('variable_3', 8), ('variable_3', 9)],
]
As a dict:
{'variable_1': 1, 'variable_2': 4, 'variable_3': 7}
{'variable_1': 1, 'variable_2': 4, 'variable_3': 8}
...
{'variable_1': 3, 'variable_2': 6, 'variable_3': 9}
如果您需要一根衬纸:
import itertools
result = map(dict, itertools.product(*[[(k, e) for e in d[k]] for k in d]))
答案 3 :(得分:0)
对于不导入的解决方案,您也可以使用递归函数:
def combo(data, c=[]):
if not data:
yield c
else:
for i in data[0]:
yield from combo(data[1:], c+[i])
d = {'variable_1': ['a1', 'b1', 'c1'], 'variable_2': ['a2', 'b2', 'c2'], 'variable_3': ['a3', 'b3', 'c3']}
keys, values = zip(*d.items())
result = [dict(zip(keys, i)) for i in combo(values)]
输出:
[{'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'a3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'b3'}, {'variable_1': 'a1', 'variable_2': 'a2', 'variable_3': 'c3'}, {'variable_1': 'a1', 'variable_2': 'b2', 'variable_3': 'a3'}, {'variable_1': 'a1', 'variable_2': 'b2', 'variable_3': 'b3'}, {'variable_1': 'a1', 'variable_2': 'b2', 'variable_3': 'c3'}, {'variable_1': 'a1', 'variable_2': 'c2', 'variable_3': 'a3'}, {'variable_1': 'a1', 'variable_2': 'c2', 'variable_3': 'b3'}, {'variable_1': 'a1', 'variable_2': 'c2', 'variable_3': 'c3'}, {'variable_1': 'b1', 'variable_2': 'a2', 'variable_3': 'a3'}, {'variable_1': 'b1', 'variable_2': 'a2', 'variable_3': 'b3'}, {'variable_1': 'b1', 'variable_2': 'a2', 'variable_3': 'c3'}, {'variable_1': 'b1', 'variable_2': 'b2', 'variable_3': 'a3'}, {'variable_1': 'b1', 'variable_2': 'b2', 'variable_3': 'b3'}, {'variable_1': 'b1', 'variable_2': 'b2', 'variable_3': 'c3'}, {'variable_1': 'b1', 'variable_2': 'c2', 'variable_3': 'a3'}, {'variable_1': 'b1', 'variable_2': 'c2', 'variable_3': 'b3'}, {'variable_1': 'b1', 'variable_2': 'c2', 'variable_3': 'c3'}, {'variable_1': 'c1', 'variable_2': 'a2', 'variable_3': 'a3'}, {'variable_1': 'c1', 'variable_2': 'a2', 'variable_3': 'b3'}, {'variable_1': 'c1', 'variable_2': 'a2', 'variable_3': 'c3'}, {'variable_1': 'c1', 'variable_2': 'b2', 'variable_3': 'a3'}, {'variable_1': 'c1', 'variable_2': 'b2', 'variable_3': 'b3'}, {'variable_1': 'c1', 'variable_2': 'b2', 'variable_3': 'c3'}, {'variable_1': 'c1', 'variable_2': 'c2', 'variable_3': 'a3'}, {'variable_1': 'c1', 'variable_2': 'c2', 'variable_3': 'b3'}, {'variable_1': 'c1', 'variable_2': 'c2', 'variable_3': 'c3'}]