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

标签 python python-2.7 dictionary

如何以数学方式将数值添加到 Python 字典中的现有键值? 例如

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

所以如果我想将 15 添加到现有 75'Motherboard' ,我该怎么办。

最佳答案

使用+= operatorinventory['Motherboard'] 作为分配的目标:

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

关于Python 字典,以数学方式将数值添加到现有键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21161575/

相关文章:

python - 如何使用 python SDK 从 Azure Batch 计算节点检索 stdout/stderr.txt 文件?

python - 如何解析具有多个根元素的xml

python - 递归复制文件

python - 如何使用 DictReader 指定要标记的字段名称?

python - 使用字典在模型中创建对象时出现 Django TypeError

python - asyncio 无法在 Google Cloud Functions 上运行

python - Seaborn:指定确切的颜色

python - 列表列表到字典到pandas DataFrame

python - odoo如何通过更新另一个字段的值来更新一个字段的值,两个字段属于不同的类

angularjs - 我如何通过 Angular js中的键获取 map 中的值