生成包含3个词典的所有组合的词典列表

时间:2019-06-19 15:44:53

标签: python

我想生成所有组合的字典列表,这些字典是从3个结合了其键的字典列表中得出的:

products = [{'product':'product1'},{'product':'product2'},{'product':'product3'},{'product':'product4'}]

sales = [{'sale':'sale1'},{'sale':'sale2'}]

invoices = [{'invoice':'invoice1'},{'invoice': 'invoice2'},{'invoice':'invoice3'}]

因此,我希望将它们合并在一起的所有组合:

combination_list = [
    {'product':'product1', 'sale':'sale1','invoice':'invoice1'},
    {'product':'product2', 'sale':'sale1','invoice':'invoice1'},
    {'product':'product3', 'sale':'sale1','invoice':'invoice1'},
    {'product':'product3', 'sale':'sale2','invoice':'invoice1'},
    {'product':'product3', 'sale':'sale3','invoice':'invoice1'},
.
.
.
.
    ]

我已经阅读了有关itertools.product的一些信息,但尚未解决。

2 个答案:

答案 0 :(得分:4)

您可以将itertools.product与字典一起使用:

from itertools import product
products = [{'product':'product1'},{'product':'product2'},{'product':'product3'},{'product':'product4'}]
sales = [{'sale':'sale1'},{'sale':'sale2'}]
invoices = [{'invoice':'invoice1'},{'invoice': 'invoice2'},{'invoice':'invoice3'}]
result = [{**a, **b, **c} for a, b, c in product(products, sales, invoices)]

输出:

[{'product': 'product1', 'sale': 'sale1', 'invoice': 'invoice1'}, {'product': 'product1', 'sale': 'sale1', 'invoice': 'invoice2'}, {'product': 'product1', 'sale': 'sale1', 'invoice': 'invoice3'}, {'product': 'product1', 'sale': 'sale2', 'invoice': 'invoice1'}, {'product': 'product1', 'sale': 'sale2', 'invoice': 'invoice2'}, {'product': 'product1', 'sale': 'sale2', 'invoice': 'invoice3'}, {'product': 'product2', 'sale': 'sale1', 'invoice': 'invoice1'}, {'product': 'product2', 'sale': 'sale1', 'invoice': 'invoice2'}, {'product': 'product2', 'sale': 'sale1', 'invoice': 'invoice3'}, {'product': 'product2', 'sale': 'sale2', 'invoice': 'invoice1'}, {'product': 'product2', 'sale': 'sale2', 'invoice': 'invoice2'}, {'product': 'product2', 'sale': 'sale2', 'invoice': 'invoice3'}, {'product': 'product3', 'sale': 'sale1', 'invoice': 'invoice1'}, {'product': 'product3', 'sale': 'sale1', 'invoice': 'invoice2'}, {'product': 'product3', 'sale': 'sale1', 'invoice': 'invoice3'}, {'product': 'product3', 'sale': 'sale2', 'invoice': 'invoice1'}, {'product': 'product3', 'sale': 'sale2', 'invoice': 'invoice2'}, {'product': 'product3', 'sale': 'sale2', 'invoice': 'invoice3'}, {'product': 'product4', 'sale': 'sale1', 'invoice': 'invoice1'}, {'product': 'product4', 'sale': 'sale1', 'invoice': 'invoice2'}, {'product': 'product4', 'sale': 'sale1', 'invoice': 'invoice3'}, {'product': 'product4', 'sale': 'sale2', 'invoice': 'invoice1'}, {'product': 'product4', 'sale': 'sale2', 'invoice': 'invoice2'}, {'product': 'product4', 'sale': 'sale2', 'invoice': 'invoice3'}]

编辑:无需拆包的解决方案:

result = [{'product':a['product'], 'sale':b['sale'], 'invoice':c['invoice']} for a, b, c in product(products, sales, invoices)]

更多通用解决方案:

result = [dict(c for b in map(dict.items, i) for c in b) for i in product(products, sales, invoices)]

答案 1 :(得分:1)

您还可以使用collections.ChainMap()合并来自itertools.product()的笛卡尔积:

from collections import ChainMap
from itertools import product
from pprint import pprint

products = [
    {"product": "product1"},
    {"product": "product2"},
    {"product": "product3"},
    {"product": "product4"},
]

sales = [{"sale": "sale1"}, {"sale": "sale2"}]

invoices = [{"invoice": "invoice1"}, {"invoice": "invoice2"}, {"invoice": "invoice3"}]

result = [dict(ChainMap(*x)) for x in product(products, sales, invoices)]

pprint(result)

输出:

[{'invoice': 'invoice1', 'product': 'product1', 'sale': 'sale1'},
 {'invoice': 'invoice2', 'product': 'product1', 'sale': 'sale1'},
 {'invoice': 'invoice3', 'product': 'product1', 'sale': 'sale1'},
 {'invoice': 'invoice1', 'product': 'product1', 'sale': 'sale2'},
 {'invoice': 'invoice2', 'product': 'product1', 'sale': 'sale2'},
 {'invoice': 'invoice3', 'product': 'product1', 'sale': 'sale2'},
 {'invoice': 'invoice1', 'product': 'product2', 'sale': 'sale1'},
 {'invoice': 'invoice2', 'product': 'product2', 'sale': 'sale1'},
 {'invoice': 'invoice3', 'product': 'product2', 'sale': 'sale1'},
 {'invoice': 'invoice1', 'product': 'product2', 'sale': 'sale2'},
 {'invoice': 'invoice2', 'product': 'product2', 'sale': 'sale2'},
 {'invoice': 'invoice3', 'product': 'product2', 'sale': 'sale2'},
 {'invoice': 'invoice1', 'product': 'product3', 'sale': 'sale1'},
 {'invoice': 'invoice2', 'product': 'product3', 'sale': 'sale1'},
 {'invoice': 'invoice3', 'product': 'product3', 'sale': 'sale1'},
 {'invoice': 'invoice1', 'product': 'product3', 'sale': 'sale2'},
 {'invoice': 'invoice2', 'product': 'product3', 'sale': 'sale2'},
 {'invoice': 'invoice3', 'product': 'product3', 'sale': 'sale2'},
 {'invoice': 'invoice1', 'product': 'product4', 'sale': 'sale1'},
 {'invoice': 'invoice2', 'product': 'product4', 'sale': 'sale1'},
 {'invoice': 'invoice3', 'product': 'product4', 'sale': 'sale1'},
 {'invoice': 'invoice1', 'product': 'product4', 'sale': 'sale2'},
 {'invoice': 'invoice2', 'product': 'product4', 'sale': 'sale2'},
 {'invoice': 'invoice3', 'product': 'product4', 'sale': 'sale2'}]