Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- my_dict = {
- 'animals' : ['dog', 'cat', 'mouse']
- }
- # Makes a list of words.
- my_str = 'john have some animals'.split()
- # Finds a matching key in my_dict based on every item
- # in my_str.
- my_choice = [my_dict.get(x, x) for x in my_str]
- # What happens is the 'animals' item in my_str matches
- # the 'animals' key in my_dict, therefore replacing the
- # 'animals' item to my_dict['animals'] (that is, ['dog',
- # 'cat', 'mouse']).
- # my_str is now ['john', 'have', 'some', ['dog', 'cat',
- # 'mouse']].
- # What you wanted is to choose an item on the list in
- # my_str, making it either:
- # 'john have some dog'
- # 'john have some cat'
- # 'john have some mouse'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement