How can I get a random value out of a dictionary in function?
Code:
a = {'tony': 'a', 'lee': 'b',
'ppark': 'c', 'anto': 'd',
'mic': 'e', 'mike': 'f'}
def test(count):
keys = random.sample(list(a), count)
values = [a[k] for k in keys]
print(dict(zip(keys, values)));
test(3)
test(3)
Result:
{'ppark': 'c', 'lee': 'b', 'tony': 'a'}
{'tony': 'a', 'ppark': 'c', 'mike': 'f'}
Aucun commentaire:
Enregistrer un commentaire