Home > Software engineering >  Explicitly remove object from list within a python dictionary
Explicitly remove object from list within a python dictionary

Time:08-03

For a dungeon crawler, I have a loot dictionary and a pack dictionary. Each contain lists. I have successfully appended random objects (which are 'found'). But, now I need to randomly remove objects (which are 'stolen'). (I will also need to explicitly remove objects for selling/substitution.) The items in the pack lists are each instances of a Weapon or other class type (each with the attribute '.item_type') which correspond to the keys. I have included some of the statements I have tried over the past few days, including del and pop, which are commented out. In particular, pop gives errors like 'TypeError: 'LeatherBoots' object cannot be interpreted as an integer'

Other similar posts address dictionaries within lists, but not lists within dictionaries.

import ...



pack = {
    'Weapons': [],
    'Healing Potions': [],
    'Armor': [],
    'Shields': [],
    'Boots': [],
    'Cloaks': [],
    'Rings of Regeneration': [],
    'Rings of Protection': [],
    'Town Portal Implements': []
}
loot_dict = {
    'Weapons': [short_sword, short_axe, quantum_sword, broad_sword],
    'Healing Potions': [minor_healing_potion, major_healing_potion, super_healing_potion],
    'Armor': [leather_armor],
    'Shields': [buckler],
    'Boots': [leather_boots],
    'Cloaks': [canvas_cloak],
    'Rings of Regeneration': [ring_of_regeneration],
    'Rings of Protection': [ring_of_protection],
    'Town Portal Implements': [scroll_of_town_portal]
}
value, key = random.choice(list(loot_dict.items()))
rndm_item = random.choice(loot_dict[value])
(pack[rndm_item.item_type]).append(rndm_item)
#(pack[rndm_item.item_type]).pop(rndm_item)
#del value
#pack.pop(random.choice(list(pack.values())))
print(rndm_item)
print(rndm_item.item_type)
print(pack)

CodePudding user response:

Do this:

# Get types of items which player have
available = []
for i in pack.keys():
    if len(pack[i]) > 0:
        available.append(i)
t = random.choice(available)   # Get an item type you want to "steal" (i.e. Weapon, Armor, etc.)
if len(available) > 0:
    if len(pack[t]) > 0:  # If the player has an item of type "t",
        pack[t].pop(random.randint(0, len(pack[t]) - 1)) # remove it
else:
 # nothing to steal

It'll remove random item from pack.

The list.pop doesn't take a value as argument, rather than an index. You tried to put a value, so nothing worked.

['a', # index: 0; value: 'a'
 'b', # index: 1, value: 'b',
 'c'  # index: 2, value: 'c']

When you put a list's item into a variable, while using del variable, you actually delete the variable itself, not an item.

CodePudding user response:

You can delete element from list with random index:

key = random.choice(list(loot_dict.keys()))
rndm_item_index = random.randrange(len(loot_dict[key]))
rndm_item = loot_dict[key][rndm_item_index]
pack[rndm_item.item_type].append(rndm_item)
del loot_dict[key][rndm_item_index]

Also you should check that loot_dict and loot_dict[key] both a not empty.

  • Related