[英]Python create combinations of dictionary
I would like to convert a Python dictionary of the following form:我想转换以下形式的 Python 字典:
D = {'a':[1,2,3], 'b':[0.1,0.5], 'c':[10,20]}
into a list of dictionaries the following form:成一个字典列表,形式如下:
E = [{a:1,b:0.1,c:10}, {a:1,b:0.1,c:20},{a:1,b:0.5,c:10}, ...., {a:3,b:0.5,c:20}]
I have tried using itertools but I do not understand how to use it to make combinations of dictionaries.我曾尝试使用 itertools,但我不明白如何使用它来组合字典。
Here is a solution:这是一个解决方案:
import itertools
D = {'a':[1,2,3], 'b':[0.1,0.5], 'c':[10,20]}
E = [dict(zip(D.keys(), a)) for a in itertools.product(*D.values())]
This yields:这产生:
E= [{'a': 1, 'b': 0.1, 'c': 10},
{'a': 1, 'b': 0.1, 'c': 20},
{'a': 1, 'b': 0.5, 'c': 10},
{'a': 1, 'b': 0.5, 'c': 20},
{'a': 2, 'b': 0.1, 'c': 10},
{'a': 2, 'b': 0.1, 'c': 20},
{'a': 2, 'b': 0.5, 'c': 10},
{'a': 2, 'b': 0.5, 'c': 20},
{'a': 3, 'b': 0.1, 'c': 10},
{'a': 3, 'b': 0.1, 'c': 20},
{'a': 3, 'b': 0.5, 'c': 10},
{'a': 3, 'b': 0.5, 'c': 20}]
Edit: Removed ordered dict as Aran points out, here is documentation to support it: Python dictionary: are keys() and values() always the same order?编辑:删除了 Aran 指出的有序字典,这里是支持它的文档: Python 字典:keys() 和 values() 总是相同的顺序吗?
Why not just:为什么不只是:
from itertools import product
D = {'a':[1,2,3], 'b':[0.1,0.5], 'c':[10,20]}
print([dict(zip(D.keys(),v)) for v in product(*D.values())])
Output:输出:
[{'a': 1, 'b': 0.1, 'c': 10}, {'a': 1, 'b': 0.1, 'c': 20}, {'a': 1, 'b': 0.5, 'c': 10}, {'a': 1, 'b': 0.5, 'c': 20}, {'a': 2, 'b': 0.1, 'c': 10}, {'a': 2, 'b': 0.1, 'c': 20}, {'a': 2, 'b': 0.5, 'c': 10}, {'a': 2, 'b': 0.5, 'c': 20}, {'a': 3, 'b': 0.1, 'c': 10}, {'a': 3, 'b': 0.1, 'c': 20}, {'a': 3, 'b': 0.5, 'c': 10}, {'a': 3, 'b': 0.5, 'c': 20}]
One option is to simply use nested for loops;一种选择是简单地使用嵌套的 for 循环; if the original data is not too big, then this will work fine and you won't need itertools
to get it working.如果原始数据不是太大,那么这将正常工作,您将不需要itertools
来使其工作。
>>> origin = {
'a': [1, 2, 3],
'b': [0.1, 0.5],
'c': [10, 20],
}
>>> result = [
{
'a': a,
'b': b,
'c': c,
}
for a in origin['a']
for b in origin['b']
for c in origin['c']]
>>> result
[{'a': 1, 'b': 0.1, 'c': 10}, {'a': 1, 'b': 0.1, 'c': 20},
{'a': 1, 'b': 0.5, 'c': 10}, {'a': 1, 'b': 0.5, 'c': 20},
{'a': 2, 'b': 0.1, 'c': 10}, {'a': 2, 'b': 0.1, 'c': 20},
{'a': 2, 'b': 0.5, 'c': 10}, {'a': 2, 'b': 0.5, 'c': 20},
{'a': 3, 'b': 0.1, 'c': 10}, {'a': 3, 'b': 0.1, 'c': 20},
{'a': 3, 'b': 0.5, 'c': 10}, {'a': 3, 'b': 0.5, 'c': 20}]
I developed a round about answer我开发了一个关于答案的回合
parameter_values_each = {'a':[1,2,3], 'b':[0.1,0.5], 'c':[10,20]}
param_possibilities = []
for name in parameter_values_each:
temp = []
for val in parameter_values_each[name]:
temp.append((name,val))
param_possibilities.append(temp)
result = list(itertools.product(*param_possibilities))
print result
Long way with no itertools (or this is what going on inside product ):很长的路没有itertools (或者这就是产品内部发生的事情):
D = {'a': [1, 2, 3], 'b': [0.1, 0.5], 'c': [10, 20]}
E = []
list_of_keys = list(D.keys())
list_of_lengths = [len(D[key]) for key in list_of_keys]
product_number = 1
for length in list_of_lengths:
product_number *= length
for n in range(product_number):
index = n
index_list = []
for length in reversed(list_of_lengths):
index_list.insert(0, index % length)
index = index // length
keys_with_values = {}
for j, key in enumerate(list_of_keys):
keys_with_values[key] = D[key][index_list[j]]
E.append(keys_with_values)
for e in E:
print(e)
Result:结果:
{'a': 1, 'b': 0.1, 'c': 10}
{'a': 1, 'b': 0.1, 'c': 20}
{'a': 1, 'b': 0.5, 'c': 10}
{'a': 1, 'b': 0.5, 'c': 20}
{'a': 2, 'b': 0.1, 'c': 10}
{'a': 2, 'b': 0.1, 'c': 20}
{'a': 2, 'b': 0.5, 'c': 10}
{'a': 2, 'b': 0.5, 'c': 20}
{'a': 3, 'b': 0.1, 'c': 10}
{'a': 3, 'b': 0.1, 'c': 20}
{'a': 3, 'b': 0.5, 'c': 10}
{'a': 3, 'b': 0.5, 'c': 20}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.