你似乎不使用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