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

How to chose between test and test1 randomly?

Uooo
  • 6,036
  • 7
  • 35
  • 62
  • What about http://stackoverflow.com/questions/4859292/how-to-get-a-random-value-in-python-dictionary? And why do you have a list of dicts, when the dicts just have one entry? – Remo Sep 12 '13 at 11:21
  • i didnt post all of the entries xD buz its many – user2772343 Sep 12 '13 at 12:27

1 Answers1

1

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 }
chepner
  • 446,329
  • 63
  • 468
  • 610
  • Thanks alot but what if it was like this, a dict has names with the their items {"raitisu": [{"name": "test"}, {"name": "test1"}],"iceyyheart": [{"name": "test"}]} and yes correct, like note = { 'name': random.choice( notes["random.name?"] )["name"] } – user2772343 Sep 12 '13 at 12:20
  • umm like you said selecting a random value for each of raitisu, iceyyheart, etc (list of user names), i have another code which send notes to the other usernames, and i dont know how choose randomly between the "user" values , to load it up then auto removing it by doing a command – user2772343 Sep 12 '13 at 12:25
  • Thanks alot but that will choose a random user then a random item/note, how can I make it not random user but instead the user who calls the cmd like "+user.name.lower()+" – user2772343 Sep 12 '13 at 12:38
  • Are there any other constraints you failed to mention in the original question? – chepner Sep 12 '13 at 12:44
  • import random name = user.name.lower() notes = {"raitisu": [{"name": "test"}, {"name": "test1"}], "iceyyheart": [{"name": "test"}]} random_note = random.choice(notes[name]) ####### will that will work? – user2772343 Sep 12 '13 at 13:34