【发布时间】:2022-01-21 18:58:10
【问题描述】:
我正在创建一个战争游戏,我有这本武器词典以及它们对另一种部队造成的伤害。
另外,我有一个列表,其中存储了字典中的键。
weapon_specs = {
'rifle': {'air': 1, 'ground': 2, 'human': 5},
'pistol': {'air': 0, 'ground': 1, 'human': 3},
'rpg': {'air': 5, 'ground': 5, 'human': 3},
'warhead': {'air': 10, 'ground': 10, 'human': 10},
'machine gun': {'air': 3, 'ground': 3, 'human': 10}
}
inv = ['rifle', 'machine gun', 'pistol']
我需要得到这个输出:
{'air': 4, 'ground': 6, 'human': 18}
我试过了:
for i in weapon_specs:
for k in inv:
if i == k:
list.update(weapon_specs[k])
【问题讨论】:
标签: python list loops dictionary for-loop