如何在Python Dictionaries中以数学方式将数值添加到现有键值中?e.g
inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}所以,如果我想在'Motherboard'的现有75上增加15,我该怎么做呢?
发布于 2014-01-16 20:20:14
使用带有inventory['Motherboard']的+= operator作为赋值的目标:
>>> inventory = {'Motherboard' : 75, 'Amplifier IC' : 55}
>>> inventory['Motherboard'] += 15
>>> inventory
{'Motherboard': 90, 'Amplifier IC': 55}https://stackoverflow.com/questions/21161575
复制相似问题