python random choice against multiple lists
Question:
I’m having trouble explaining this so please bare with me.
I have several lists and I want to write a python script that picks an item at random from the first list, then checks that result against a "fail list," if the item isn’t on the "fail list" I want to move to the next list and do the same thing until it fails.
#Go through these lists one by one, picking random item.
action_heros = ['thor', 'batman', 'spiderman', 'superbart']
friends = ['joey', 'feebee', 'rachael', 'dog']
himym = ['robin', 'marshall', 'ted', 'lily', 'barney']
# fail list
simpsons = ['bart', 'homer', 'marg', 'superbart', 'dog', 'barney']
#This is how I've been trying to solve it, but I can't get the code to move to the second or third attempt
# pick a random word from the lists
rand_action_hero = random.choice(action_heros)
rand_friend = random.choice(friends)
rand_himym = random.choice(himym)
#run the random word from each list against the checklist one by one until it fails.
if rand_action_hero in simpsons:
print('failed at actionheros')
#if the random word isn't in simpsons I want it to pick a new random word and try against friend, then himym
Thanks for any help, I’m still learning to code so it means a lot!
Answers:
Put all your choices into a list, then loop through them testing if it’s in the fail list.
for listname, choice in [('action_heros', rand_action_hero), ('friends', rand_friend), ('himym', rand_himym)]:
if choice in simpsons:
print(f'failed at {listname}')
break
else:
print('All tests succeeded')
You could avoid hard-coding all the list names and variables by turning the separate lists into a dictionary.
Instead of repeatedly picking a new choice, I’d suggest removing all items in the fail-list from your input.
For example:
action_heros = [item for item in ['thor', 'batman', 'spiderman', 'superbart'] if item not in simpsons]
['thor', 'batman', 'spiderman']
This makes sure you always get a valid choice.
simpsons = ['bart', 'homer', 'marg', 'superbart', 'dog', 'barney']
action_heros = ['thor', 'batman', 'spiderman', 'superbart']
random.choice([item for item in action_heros if item not in simpsons])
You can do the same by converting your input to sets and use the setdifference:
random.choice(tuple(set(action_heros) - set(simpsons)))
If you know about hash sets, I would use a set for the fail list for faster lookups although it is unnecessary.
Other than that, this code should work.
import random
#Go through these lists one by one, picking random item.
action_heros = ['thor', 'batman', 'spiderman', 'superbart']
friends = ['joey', 'feebee', 'rachael', 'dog']
himym = ['robin', 'marshall', 'ted', 'lily', 'barney']
# fail list
simpsons = set(['bart', 'homer', 'marg', 'superbart', 'dog', 'barney'])
# loop until it breaks
while 1:
rand_action_hero = random.choice(action_heros)
print(rand_action_hero)
if rand_action_hero in simpsons:
print("random action hero in fail list")
break
rand_friend = random.choice(friends)
print(rand_friend)
if rand_friend in simpsons:
print("random friend in fail list")
break
rand_himym = random.choice(himym)
print(rand_himym)
if rand_himym in simpsons:
print("random himym in fail list")
break
Explanation:
We first create a dictionary with all the lists combined with their respectful title, and we loop over each element in that dictionary and compare a random.choice()
from that element with the fail_list
. and if it exists in the fail list we continue the loop, now if it doesn’t exist in the loop we break/stop that loop.
My code:
import random
lists = {"action heros":['thor', 'batman', 'spiderman', 'superbart'],
"friends":['joey', 'feebee', 'rachael', 'dog'],
"himym":['robin', 'marshall', 'ted', 'lily', 'barney']}
fail_list = ['bart', 'homer', 'marg', 'superbart', 'dog', 'barney']
for elements in lists:
if random.choice(lists[elements]) in fail_list:
continue
else:
print("failed at {0}".format(elements))
break
Output
- code won’t have any fixed output since it runs with a
random.choice()
but…
>>> failed at himym
Diagram
I’m having trouble explaining this so please bare with me.
I have several lists and I want to write a python script that picks an item at random from the first list, then checks that result against a "fail list," if the item isn’t on the "fail list" I want to move to the next list and do the same thing until it fails.
#Go through these lists one by one, picking random item.
action_heros = ['thor', 'batman', 'spiderman', 'superbart']
friends = ['joey', 'feebee', 'rachael', 'dog']
himym = ['robin', 'marshall', 'ted', 'lily', 'barney']
# fail list
simpsons = ['bart', 'homer', 'marg', 'superbart', 'dog', 'barney']
#This is how I've been trying to solve it, but I can't get the code to move to the second or third attempt
# pick a random word from the lists
rand_action_hero = random.choice(action_heros)
rand_friend = random.choice(friends)
rand_himym = random.choice(himym)
#run the random word from each list against the checklist one by one until it fails.
if rand_action_hero in simpsons:
print('failed at actionheros')
#if the random word isn't in simpsons I want it to pick a new random word and try against friend, then himym
Thanks for any help, I’m still learning to code so it means a lot!
Put all your choices into a list, then loop through them testing if it’s in the fail list.
for listname, choice in [('action_heros', rand_action_hero), ('friends', rand_friend), ('himym', rand_himym)]:
if choice in simpsons:
print(f'failed at {listname}')
break
else:
print('All tests succeeded')
You could avoid hard-coding all the list names and variables by turning the separate lists into a dictionary.
Instead of repeatedly picking a new choice, I’d suggest removing all items in the fail-list from your input.
For example:
action_heros = [item for item in ['thor', 'batman', 'spiderman', 'superbart'] if item not in simpsons]
['thor', 'batman', 'spiderman']
This makes sure you always get a valid choice.
simpsons = ['bart', 'homer', 'marg', 'superbart', 'dog', 'barney']
action_heros = ['thor', 'batman', 'spiderman', 'superbart']
random.choice([item for item in action_heros if item not in simpsons])
You can do the same by converting your input to sets and use the setdifference:
random.choice(tuple(set(action_heros) - set(simpsons)))
If you know about hash sets, I would use a set for the fail list for faster lookups although it is unnecessary.
Other than that, this code should work.
import random
#Go through these lists one by one, picking random item.
action_heros = ['thor', 'batman', 'spiderman', 'superbart']
friends = ['joey', 'feebee', 'rachael', 'dog']
himym = ['robin', 'marshall', 'ted', 'lily', 'barney']
# fail list
simpsons = set(['bart', 'homer', 'marg', 'superbart', 'dog', 'barney'])
# loop until it breaks
while 1:
rand_action_hero = random.choice(action_heros)
print(rand_action_hero)
if rand_action_hero in simpsons:
print("random action hero in fail list")
break
rand_friend = random.choice(friends)
print(rand_friend)
if rand_friend in simpsons:
print("random friend in fail list")
break
rand_himym = random.choice(himym)
print(rand_himym)
if rand_himym in simpsons:
print("random himym in fail list")
break
Explanation:
We first create a dictionary with all the lists combined with their respectful title, and we loop over each element in that dictionary and compare a random.choice()
from that element with the fail_list
. and if it exists in the fail list we continue the loop, now if it doesn’t exist in the loop we break/stop that loop.
My code:
import random
lists = {"action heros":['thor', 'batman', 'spiderman', 'superbart'],
"friends":['joey', 'feebee', 'rachael', 'dog'],
"himym":['robin', 'marshall', 'ted', 'lily', 'barney']}
fail_list = ['bart', 'homer', 'marg', 'superbart', 'dog', 'barney']
for elements in lists:
if random.choice(lists[elements]) in fail_list:
continue
else:
print("failed at {0}".format(elements))
break
Output
- code won’t have any fixed output since it runs with a
random.choice()
but…
>>> failed at himym