Pregunta

{"raitisu": [{"name": "test"}, {"name": "test1"}]}         
note = {'name': ""+random.choice(notes.keys())+""}

How to chose between test and test1 randomly?

¿Fue útil?

Solución

Choose a list of dictionaries at random from notes (using the fact that a dictionary yields its keys when treated as an iterable), then choose a value from the list using the known key "name".

import random
notes = {"raitisu": [{"name": "test"}, {"name": "test1"}],
         "iceyyheart": [{"name": "test"}]}
notes_key = random.choice(notes)
random_dict_list = notes[notes_key]
random_value = random.choice(random_dict_list)["name"]
note = { 'name': random_value }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top