Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- myDict = {"IronSword" : 15, "SteelSword" : 30, "Playing Cards" : 45, "Spider legs" : 60"}
- d = random.functionNameHere(myDict, 1) # Same arguments as random.sample()
- print d
- items = list(myDict.items()) # list of key/value pairs
- key, value = random.choice(items) # pick a random pair
- print {key: value} # make a dictionary out of the result
- >>> dict(random.sample(myDict.items(), 1))
- {'Playing Cards': 45}
- from random import choice
- d = dict([choice(myDict.items())])
- print d
- def getOne():
- k = random.choice(myDict.keys())
- v = myDict[k]
- return (k,v)
- >>> getOne()
- ('SteelSword', 30)
- >>> getOne()
- ('Spiderlegs', 60)
- >>> getOne()
- ('Spiderlegs', 60)
- >>> getOne()
- ('SteelSword', 30)
- >>> getOne()
- ('Playing Cards', 45)
- import random
- myDict = {"IronSword" : 15, "SteelSword" : 30, "Playing Cards" : 45, "Spider legs" : 60}
- random_keys = random.sample(myDict.keys(), 2)
- d = {k: myDict[k] for k in random_keys}
- return d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement