你似乎不使用Python字典,這應該工作:
operation = [[Brand1, operationCost], [Brand2, operationCost],...]
maintenance = [[Brand1, maintenanceCost], [Brand2, maintenanceCost],...]
replacement = [[Brand1, replacementCost], [Brand2, replacementCost],...]
total = [ [ope[0], ope[1]+mai[1]+rep[1]] for ope,mai,rep in zip(operation,maintenance,replacement) ]
編輯:
但是你不能用上面這段代碼,如果列出的lenght或順序品牌改變。所以最好的解決方案是使用字典:
# Your matrix as dictionaries
operation = {Brand1: operationCost, Brand2: operationCost, ...}
maintenance = {Brand1: maintenanceCost, Brand2: maintenanceCost, ...}
replacement = {Brand1: replacementCost, Brand2: replacementCost, ...}
# Get all brands in a container
all_brands = set(operation.keys()+maintenance.keys()+replacement.keys())
# Return 0 as default value if a brand is not in the dictionary
f = lambda x, dic: dic[x] if x in dic else 0
# Compute the total cost of each brand
total = {brand: f(brand,operation)+f(brand,maintenance)+f(brand,replacement) for brand in all_brands}
或爲2.7版本之前的Python:
total = dict([(brand, f(brand,operation)+f(brand,maintenance)+f(brand,replacement)) for brand in all_brands])
一個我有一個問題是,我不能導入numpy的或大熊貓庫在我的項目。 – Madmartigan