Advertisement
Guest User

Untitled

a guest
Apr 21st, 2014
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. import random
  2. myDict = {"IronSword" : 15, "SteelSword" : 30, "Playing Cards" : 45, "Spider legs" : 60"}
  3. d = random.functionNameHere(myDict, 1) # Same arguments as random.sample()
  4. print d
  5.  
  6. items = list(myDict.items()) # list of key/value pairs
  7. key, value = random.choice(items) # pick a random pair
  8. print {key: value} # make a dictionary out of the result
  9.  
  10. >>> dict(random.sample(myDict.items(), 1))
  11. {'Playing Cards': 45}
  12.  
  13. from random import choice
  14. d = dict([choice(myDict.items())])
  15. print d
  16.  
  17. def getOne():
  18. k = random.choice(myDict.keys())
  19. v = myDict[k]
  20. return (k,v)
  21.  
  22. >>> getOne()
  23. ('SteelSword', 30)
  24. >>> getOne()
  25. ('Spiderlegs', 60)
  26. >>> getOne()
  27. ('Spiderlegs', 60)
  28. >>> getOne()
  29. ('SteelSword', 30)
  30. >>> getOne()
  31. ('Playing Cards', 45)
  32.  
  33. import random
  34. myDict = {"IronSword" : 15, "SteelSword" : 30, "Playing Cards" : 45, "Spider legs" : 60}
  35. random_keys = random.sample(myDict.keys(), 2)
  36. d = {k: myDict[k] for k in random_keys}
  37. return d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement