我是库存经理,我的库存如下
oldstock = {"A":100,"B":120,"C":150,"D":100,"E":230,"F":200,"G":180,"H":140,"I":90,"J":50}
但是,我从下面的每个容器中出售了一些物品(似乎字典的表述是最好的表示方式)
sale = [{"A":20},{"C":25},{"E":15},{"F":18},{"H":20},{"C":35},{"A":40},{"A":5},{"E":40},{"H":20}]
每箱销售完成后如何计算新库存?
答案 0 :(得分:-1)
更简洁易懂的方法如下:
for item in sale:
for k, v in item.items():
# an extra check, you can remove this if you know for sure that sale will never have any key which is not in oldstock
if k in oldstock:
oldstock[k] = oldstock[k] - v
print(oldstock)
# {'A': 35, 'B': 120, 'C': 90, 'D': 100, 'E': 175, 'F': 182, 'G': 180, 'H': 100, 'I': 90, 'J': 50}
希望这会有所帮助。祝你好运。