dimanche 10 janvier 2021

Use random samples as a function in Python [closed]

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