After concatenating several lists how can I know to which of the original lists an item belong to?

Viewed 58

I combined many lists from other different types of lists, and when I get an output, how do I tell Python what list from the combined list this is from?

For example, I have multiple lists here.

vampire_weapon = ['stake','silver sword'];
vampire_random = random.randint(0,1)

ghost_weapon = ['flash light','machine'];
ghost_random = random.randint(0,1)

zombie_weapon = ['crossbow','gun'];
zombie_random = random.randint(0,1)

monster_list = ['vampire', 'ghost', 'zombie']
monster_random = random.randint(0,2)

free_move = ['free move foward']

weapon_list = vampire_weapon + ghost_weapon + zombie_weapon
weapon_random = random.randint(0,8)

big_list = weapon_list + monster_list + free_move
big_random = random.randint(0,9)

And it is visible at the bottom that I have made a variable called big_list. When I run my code I get an output randomly selected from any of the lists above this code. How do I code it so that Python knows what list this is from so I can make different outputs like the example below?

For example if it's from the weapon_list, I can output "You encountered a (insert weapon)". And if it's from the monster_list I can output "You bumbed into a (insert monster)".

4 Answers

Once you have selected the weapon then you can:

weapon = big_list[big_random]

if weapon in vampire_weapon:
    print("You encountered a vampire weapon")
elif weapon in ghost_weapon:
    print("You bumbed into a ghost weapon")
...

And I would recommend you to select the random index differently, based on the size of the big_list, not just a hardcode number:

big_random = random.randint(0,len(big_list))

I would change the logic of picking random weapon. Create dictionary for all of weapons:

weapons = {
"vampire_weapon": ['stake','silver sword'],
"ghost_weapon": ['flash light','machine'],
"zombie_weapon" : ['crossbow','gun'],
"monster_list": ['vampire', 'ghost', 'zombie'],
"free_move": "free move forward"

}

pick from here random key using random library:

random_value = random.choice(list(weapons.items()))
random_type = random_value[0]
random_weapons = random_value[1] #next you should pick one weapon from here

if random_type != "free_move":
     random_weapon = random.choice(random_weapons)

so you already know where from is your weapon chosen. Also you can make some dictionary for certain messages for example

messages_dict = {
"monster_list": "You bumbed into a (insert monster)"
}

and when you need to get message just use: messages_dict[random_type]

Put all your monsters in a list of dicts and pic them random:

Example

import random

lst = [
    {'monster':'vampire', 'weapons':['stake','silver sword']},
    {'monster':'ghost', 'weapons':['flash light','machine']},
    {'monster':'zombie', 'weapons':['crossbow','gun']}
]

monster = random.choice(lst)
weapon = random.choice(monster['weapons'])

print('You encountered a {0} and bumbed into a {1}'.format(weapon, monster['monster']))

Output

You encountered a gun and bumbed into a zombie

You can do all this in one go.

Create a dictionary of all the categories of weapons, the list of each of the weapons in the category, and the response you want to add to the pick.

This solution allows you to keep adding more weapon categories, weapons, and response. The code does not have to change and you don't need to add more code to manage the conditions. All you are doing is adding the category, weapon list, and response to the all_vals dictionary.

To help you understand how it works, here's an example.

import random
all_vals = {'vampires':(['stake','silver sword'],'bumbed'),
            'ghosts':(['flash light','machine'],'ghosted'),
            'zombie':(['crossbow','gun'],'zee')}
weapon_cat  = random.choice(list(all_vals))
weapon_type = random.choice(list(all_vals[weapon_cat][0]))
weapon_resp = all_vals[weapon_cat][1]
print (weapon_cat, weapon_type, weapon_resp)

Sample outputs are:

zombie gun zee
ghosts flash light ghosted
vampires silver sword bumbed
Related