This sort of problem is an ideal use-case for a dictionary. A dictionary is a lot like an array, but instead of using sequential numbers 0, 1, 2...
as keys (which would be an array) you can use arbitrary strings. So you can have, for example,
products = {
'1a1a': MyDrillObject,
'2b2b': MyScrewsProduct,
'fffee': MyTowelProduct
}
and then to retrieve a specific object representing a product you can do, for example,
drill_obj = products['1a1a']
And this lookup is efficient, it doesn't iterate through all the keys in the dictionary.
So to solve your problem, you could do away with your product list index and instead use the ProductID
as the dictionary key directly.
This would do simplify the preprocessing step down to just adding all the products to the product dictionary:
product_dict = {p.ProductID: p for p in products}
and then when processing transactions, you can use the productID directly:
product_dict[transaction.ProductID].total_gallons_sold_of_this_product += x
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…