jeudi 18 janvier 2018

Fetching n random keys with certain values from dictionary

How can I fetch N random keys from a dictionary with a certain value. Dictionary looks like this:

d = {'t1': {'text': 'blabla', 'rating': '1'}, 't2': {'text': 'blabla', 'rating': '0'}, 't3': {'text': 'blabla', 'rating': '0'}, 't4': {'text': 'blabla', 'rating': '1'}, 't5': {'text': 'blabla', 'rating': '1'}}

For example, I want 2 random keys from d, where the value for rating is 1.




Aucun commentaire:

Enregistrer un commentaire