Advertisement
Guest User

Untitled

a guest
May 16th, 2018
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import random
  2.  
  3. my_dict = {
  4.     'animals' : ['dog', 'cat', 'mouse']
  5.     }
  6.  
  7. # Makes a list of words.
  8. my_str = 'john have some animals'.split()
  9.  
  10. # Finds a matching key in my_dict based on every item
  11. # in my_str.
  12. my_choice = [my_dict.get(x, x) for x in my_str]
  13.  
  14. # What happens is the 'animals' item in my_str matches
  15. # the 'animals' key in my_dict, therefore replacing the
  16. # 'animals' item to my_dict['animals'] (that is, ['dog',
  17. # 'cat', 'mouse']).
  18.  
  19. # my_str is now ['john', 'have', 'some', ['dog', 'cat',
  20. # 'mouse']].
  21.  
  22. # What you wanted is to choose an item on the list in
  23. # my_str, making it either:
  24. # 'john have some dog'
  25. # 'john have some cat'
  26. # 'john have some mouse'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement