如何以数学方式将数值添加到 Python 字典中的现有键值? 例如
inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}
所以如果我想将 15 添加到现有 75
的'Motherboard'
,我该怎么办。
最佳答案
使用+=
operator以 inventory['Motherboard']
作为分配的目标:
>>> inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}
>>> inventory['Motherboard'] += 15
>>> inventory
{'Motherboard': 90, 'Amplifier IC': 55}
关于Python 字典,以数学方式将数值添加到现有键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21161575/