如何在Python Dictionaries中以数学方式将数值添加到现有键值? e.g
inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}
因此,如果我想向75
的{{1}}添加15,我该怎么办呢。
答案 0 :(得分:2)
将+=
operator与inventory['Motherboard']
一起用作作业的目标:
>>> inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}
>>> inventory['Motherboard'] += 15
>>> inventory
{'Motherboard': 90, 'Amplifier IC': 55}