Python:如何从字典键中随机 select 一个值?
Python: How do I randomly select a value from a dictionary key?
我有字典:
dict = {
"Apple": ["Green", "Healthy", "Sweet"],
"Banana": ["Yellow", "Squishy", "Bland"],
"Steak": ["Red", "Protein", "Savory"]
}
我想从每个键中打印一个随机值,所以我首先尝试将它们放入列表中:
import random
food = [dict.value.random.choice()]
但这行不通(毫不奇怪,它看起来过多且令人困惑)
然后我想 print
食物:
print food
看看:
green
squishy
savory
或随机选择的任何值。
创建列表是不必要的吗?我会继续发布尝试。
只是为了澄清为什么这不是重复项:我不想从字典中随机抓取一个项目,我想从字典中的每个列表中随机抓取一个项目。
您可以使用列表理解来遍历您的值:
>>> 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']
要像您想要的那样打印,您可以使用 join
函数或循环遍历 food
并一一打印元素:
>>> print '\n'.join(food)
Savory
Green
Squishy
>>> for val in food :
... print val
...
Savory
Green
Squishy
您还可以使用 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
结果:
Red Steak
Healthy Apple
Bland Banana
使用列表推导式
import random
choices = [random.choice(v) for k, v in your_dict.items()] # iterate over the dict items
print(choices)
输出
['Protein', 'Green', 'Squishy']
(顺便说一句,将名称 '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)])
输出(会根据随机int值变化)
sh-4.2# python3 main.py
Apple:Sweet
Steak:Red
Banana:Squishy
修正案01(@Selcuk的提问)
for key, value in data.items():
length = len(value)
print(key + ":" + value[randint(0,length-1)])
在彻底浏览文档并使用上面提供的所有非常有用的答案之后,这是我发现最干净和明显的答案:
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
我有字典:
dict = {
"Apple": ["Green", "Healthy", "Sweet"],
"Banana": ["Yellow", "Squishy", "Bland"],
"Steak": ["Red", "Protein", "Savory"]
}
我想从每个键中打印一个随机值,所以我首先尝试将它们放入列表中:
import random
food = [dict.value.random.choice()]
但这行不通(毫不奇怪,它看起来过多且令人困惑)
然后我想 print
食物:
print food
看看:
green
squishy
savory
或随机选择的任何值。
创建列表是不必要的吗?我会继续发布尝试。
只是为了澄清为什么这不是重复项:我不想从字典中随机抓取一个项目,我想从字典中的每个列表中随机抓取一个项目。
您可以使用列表理解来遍历您的值:
>>> 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']
要像您想要的那样打印,您可以使用 join
函数或循环遍历 food
并一一打印元素:
>>> print '\n'.join(food)
Savory
Green
Squishy
>>> for val in food :
... print val
...
Savory
Green
Squishy
您还可以使用 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
结果:
Red Steak
Healthy Apple
Bland Banana
使用列表推导式
import random
choices = [random.choice(v) for k, v in your_dict.items()] # iterate over the dict items
print(choices)
输出
['Protein', 'Green', 'Squishy']
(顺便说一句,将名称 '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)])
输出(会根据随机int值变化)
sh-4.2# python3 main.py
Apple:Sweet
Steak:Red
Banana:Squishy
修正案01(@Selcuk的提问)
for key, value in data.items():
length = len(value)
print(key + ":" + value[randint(0,length-1)])
在彻底浏览文档并使用上面提供的所有非常有用的答案之后,这是我发现最干净和明显的答案:
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