Python字典,以数学方式将数值添加到现有键值

时间:2014-01-16 12:18:22

标签: python python-2.7 dictionary

如何在Python Dictionaries中以数学方式将数值添加到现有键值? e.g

inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}

因此,如果我想向75的{​​{1}}添加15,我该怎么办呢。

1 个答案:

答案 0 :(得分:2)

+= operatorinventory['Motherboard']一起用作作业的目标:

>>> inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}
>>> inventory['Motherboard'] += 15
>>> inventory
{'Motherboard': 90, 'Amplifier IC': 55}