如何将第一个字典中的值添加到第二个字典中的值

How to add values from first dict to values in second dict

我卡住了,几乎不需要帮助。

如果我有一个字典和其他词典,我如何添加值?

For example:

player_equipment = {'Boots:': [('Defence', 4), ('Mana', 13), ('Stamina', 30)], 'Gloves:': [('Attack', 5), ('Defence', 1), ('Health', 19), ('Mana', 3), ('Stamina', 29)]}

I need a result:

player_equipment_statictic = {'Attack':5, 'Defence':5,'Health':19 'Mana':16, 'Stamina':59}  

在接下来的步骤中,我将尝试将 player_equipment_statictic 添加到 player_stats,玩家将在 class Player:

中获得他的统计数据
class Player:
    def __init__(self, health=100, max_health=1300, mana=100, stamina=222, attack=10, defence=1, magic=11, lucky=1, level=1, experience=95, points=1):
        self.health = health
        self.max_health = max_health
        self.mana = mana
        self.stamina = stamina
        self.defence = defence
        self.attack = attack
        self.magic = magic
        self.lucky = slucky
        self.level = level
        self.experience = experience
        self.points = points
player_equipment = {'Boots:': [('Defence', 4), ('Mana', 13), ('Stamina', 30)],
                    'Gloves:': [('Attack', 5), ('Defence', 1), ('Health', 19), ('Mana', 3), ('Stamina', 29)]}
player_equipment_statictic = {}

# iterating through player_equipment extracting item list
for _, item_list in player_equipment.items():
    # iterating through item_list extracting pairs of item and count
    for item, count in item_list:
        # checking if item exists in new dictionary
        if item in player_equipment_statictic:
            # updating item in the new dictionary
            player_equipment_statictic[item] += count
        # if item not exists in the new dictionary
        else:
            # creating new item in the new dictionary with the count as value
            player_equipment_statictic[item] = count

print(player_equipment_statictic)

输出

{'Defence': 5, 'Mana': 16, 'Stamina': 59, 'Attack': 5, 'Health': 19}