简体   繁体   English

Python:如何从字典键中随机选择一个值?

[英]Python: How do I randomly select a value from a dictionary key?

I have a dictionary: 我有一本字典:

dict = {
    "Apple": ["Green", "Healthy", "Sweet"],
    "Banana": ["Yellow", "Squishy", "Bland"],
    "Steak": ["Red", "Protein", "Savory"]
}

and I want to print one random value from each key, so I tried to get them into a list first: 我想从每个键中打印一个随机值,所以我试着先将它们放入列表中:

import random

food = [dict.value.random.choice()]

but this doesn't work (no surprise, it looks excessive and confusing) 但这不起作用(毫不奇怪,它看起来过分和令人困惑)

and I want to then print food: 然后我想print食物:

print food

and just see: 并看到:

green
squishy
savory

or whatever value was randomly selected. 或随机选择的任何值。

is creating the list unnecessary? 正在创建不必要的列表? I'll keep posting attempts. 我会继续发布尝试。

Just to clarify why this is not a duplicate: I don't want to randomly grab an item from a dictionary, I want to randomly grab an item from a each list inside a dictionary. 只是为了澄清为什么这不是重复:我不想从字典中随机抓取一个项目,我想从字典中的每个列表中随机抓取一个项目。

You can use a list comprehension to loop over your values : 您可以使用列表推导来循环您的值:

>>> my_dict = {
...     "Apple": ["Green", "Healthy", "Sweet"],
...     "Banana": ["Yellow", "Squishy", "Bland"],
...     "Steak": ["Red", "Protein", "Savory"]
... }
>>> import random
>>> food=[random.choice(i) for i in my_dict.values()]
>>> food
['Savory', 'Green', 'Squishy']

And for print like what you want you can use join function or loop over food and print the elements one by one : 对于你想要的打印,你可以使用join功能或循环food并逐个打印元素:

>>> print '\n'.join(food)
Savory
Green
Squishy
>>> for val in food :
...      print val
... 
Savory
Green
Squishy

You can also use a for loop: 您还可以使用for循环:

import random

dict = {
    "Apple": ["Green", "Healthy", "Sweet"],
    "Banana": ["Yellow", "Squishy", "Bland"],
    "Steak": ["Red", "Protein", "Savory"]
}

for key, value in dict.items():
    print random.choice(value), key

result: 结果:

Red Steak
Healthy Apple
Bland Banana

Use a list comprehension as in 使用列表推导,如

import random
choices = [random.choice(v) for k, v in your_dict.items()] # iterate over the dict items
print(choices)

Output 产量

['Protein', 'Green', 'Squishy']

(BTW-Change the name 'dict' to something else) (BTW-将名称'dict'更改为其他内容)

# for python3
from random import randint
data = {
  "Apple": ["Green", "Healthy", "Sweet"],
  "Banana": ["Yellow", "Squishy", "Bland"],
  "Steak": ["Red", "Protein", "Savory"]
}

for  key, value in data.items():
  print(key + ":" + value[randint(0,2)])

Output (will change depending on the random int values) 输出 (将根据随机int值改变)

sh-4.2# python3 main.py                                                                                                                                                         
Apple:Sweet                                                                                                                                                                     
Steak:Red                                                                                                                                                                       
Banana:Squishy                                                                                                                                                                    

Amendment 01 (@ Selcuk's question) 修正案01(@ Selcuk的问题)

for  key, value in data.items():
  length = len(value)
  print(key + ":" + value[randint(0,length-1)])

After thoroughly going through docs and using all of the very useful answers provided above, this is what I found to be the most clean and obvious: 在彻底浏览了文档并使用上面提供的所有非常有用的答案之后,我发现这是最干净和最明显的:

import random  

food_char = {
    "Apple": ["Green", "Healthy", "Sweet"],
    "Banana": ["Yellow", "Squishy", "Bland"],
    "Steak": ["Red", "Protein", "Savory"]
}

food=[random.choice(i) for i in food_char.values()]

for item in food:
    print item

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM