Dragoonknight Dragoonknight - 5 months ago 37
Python Question

How do I copy values from one dictionary to a second dictionary with new keys?

I am trying to make weapons in my game exclusive to certain classes. I have an item database in form:

itemsList = {
1: {"name": "Padded Armor", "armor": 1, "value": 5, "class": "light"},
2: {"name": "Leather Armor", "armor": 2, "value": 10, "class": "light"},
3: {"name": "Studded Leather Armor", "armor": 3, "value": 25, "class": "light"},
19: {"name": "Dagger", "damage" : int(random.randrange(1, 4)), "value": 2, "Type": "Dagger"},
20: {"name": "Dagger + 1", "damage" : int(random.randrange(1, 4) + 1), "value": 200, "Type": "Dagger"},
21: {"name": "Dagger + 2", "damage" : int(random.randrange(1, 4) + 2), "value": 750, "Type": "Dagger"},
22: {"name": "Dagger + 3", "damage" : int(random.randrange(1, 4) + 3), "value": 2000, "Type": "Dagger"}

I am going to import, if a class can equip a certain weapon, to a new dictionary,

character.canEquipWeapon = {}

I would like to assign the name, damage, value, and type to a new set of keys in the dictionary, only, I would like to add only certain subsets (daggers, maces, swords) to certain classes.

I have tried


and that just overwrites the dictionary with each new value. How do I go about doing this?


You could create a dict with usable items by class, so that for a given class, you have the list of the IDs it can equip as such :

classItem = {
          'rogue'   : [1,2,3,12,13,14,15], 
          'mage'    : [13,15,16,18], 
          'soldier' : [1,2,3,4,5,6,7],

Then to get the list of items usable by the rogue for instance :

rogueItems = [v for k,v in itemsList.items() if k in classItem['rogue']]

Note that with python2 you'd have to use itemsList.iteritems() instead of itemsList.items()