I am looking for a fast way to build export.
Got three array as input data - two regular arrays and one array of dictionaries:
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' array may contain from 0 to 20,000 records. I need a fast way to build the following export:
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}
]
I tried using this way, but it is very slow:
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})
Thank you!
This is python3 (sorry, i don't have a python 2 compiler so i can't check), but is very basic and maybe you can adopt it. It is faster because I create a set out of mappings to avoid running filter all the time.