寻找一种快速构建所有可能性导出的方法 (Python 2.5)

Looking for a fast way to build export of all possibilities (Python 2.5)

我正在寻找一种快速构建导出的方法。

获得了三个数组作为输入数据 - 两个常规数组和一个字典数组:

companies = ["company1", "company2", "company3", "company4", "company5", "company6", "company7", "company8", "company9"]
products = ["product1", "product2", "product3", "product4", "product5"]
mappings = [{"company": "company1", "product": "product1"},
            {"company": "company1", "product": "product2"},
            {"company": "company1", "product": "product3"},
            {"company": "company3", "product": "product6"},
            {"company": "company3", "product": "product9"},
            {"company": "company4", "product": "product2"}
            ]

'mappings' 数组可能包含 0 到 20,000 条记录。 我需要一种快速构建以下导出的方法:

mappings_export = [{"company": "company1", "product": "product1", "is_mapped": 1},
                   {"company": "company1", "product": "product2", "is_mapped": 1},
                   {"company": "company1", "product": "product3", "is_mapped": 1},
                   {"company": "company1", "product": "product4", "is_mapped": 0},
                   {"company": "company1", "product": "product5", "is_mapped": 0},
                   {"company": "company1", "product": "product6", "is_mapped": 1}
                   ]

我试过用这种方式,但是很慢:

mappings_export = []
for company in companies:
    for product in products:
        found = filter(lambda x: x["product"] == product and x["company"] == company, mappings)
        if len(found) > 0:
            mapped = 1
        else:
            mapped = 0
        mappings_export.append({"company": company,
                                "product": product,
                                "mapped": mapped})

谢谢!

这是 python3(抱歉,我没有 python 2 编译器,所以我无法检查),但是非常基础,也许你可以采用它。它更快,因为我创建了一组映射以避免一直 运行 过滤。

company_product = set([])

for m in mappings:
    company_product.add((m["company"],m["product"]))

mappings_export = []
for c in companies:
    for p in products:
        mappings_export.append({"company": c,
                                "product": p,
                                "mapped": 1 if (c,p) in company_product else 0})