简体   繁体   English

Python:如何从字典中随机选择的键中获取值

[英]Python: How to get a value from randomly selected key in a dictionary

What I'm trying to do here is to get a random choice from a list我在这里要做的是从列表中随机选择

races = ['Dragonborn', 'Dwarf', 'Elf', 'Gnome', 'Half-Elf', 'Halfling', 'Half-Orc', 'Human', 'Tiefling']
races_choice = random.choice(races)

and then use that random choice from the list to look at keys in a separate dictionary然后使用列表中的随机选择来查看单独字典中的键

subraces = {
'Dwarf': ['Hill Dwarf', 'Mountain Dwarf'],
'Elf': ['High Elf', 'Wood Elf', 'Dark Elf'], 
'Halfling': ['Lightfoot', 'Stout'], 
'Gnome': ['Forest Gnome', 'Rock Gnome'],
}

and if that key matches the random choice, print a random value from that key.如果该键与随机选择匹配,则从该键中打印一个随机值。

I've tried a few things, but what I'm currently on is this:我已经尝试了一些事情,但我目前正在做的是:

if races_choice == subraces.keys():
    print(random.choice(subraces.values[subraces.keys]))

But this returns nothing.但这没有任何回报。 I'm a bit at a loss for how to do this.我对如何做到这一点有点不知所措。

Thank you.谢谢你。

You can simply use .get on the dictionary.您可以简单地在字典上使用.get

DEFAULT_VALUE = "default value"
subraces = {
       'Dwarf': ['Hill Dwarf', 'Mountain Dwarf'],
       'Elf': ['High Elf', 'Wood Elf', 'Dark Elf'], 
       'Halfling': ['Lightfoot', 'Stout'], 
       'Gnome': ['Forest Gnome', 'Rock Gnome'],
}

races = ['Dragonborn', 'Dwarf', 'Elf', 'Gnome', 'Half-Elf', 
        'Halfling', 
        'Half-Orc', 'Human', 'Tiefling']

races_choice = random.choice(races)
subrace_options = subraces.get(races_choice, DEFAULT_VALUE)
if subrace_options != DEFAULT_VALUE:
    index = random.randint(0, (len(subrace_options) - 1))
    print(subrace_options[index])
else: 
    print("No subrace for specified race")

This will yield the name of a subrace from a given race eg for Dwarf the output will be a random entry in the list ie Hill Dwarf .这将产生来自给定种族的子种族的名称,例如对于Dwarf ,output 将是列表中的随机条目,即Hill Dwarf

The string value in the .get is the default value which will be assigned if the key (the random choice) cannot be found in your map of subraces. .get中的字符串值是默认值,如果在子种族的 map 中找不到密钥(随机选择),则将分配该值。

It seems like you can simply set a random int equivalent to the length of items accordingly to the key似乎您可以根据键简单地设置一个随机 int 等效于项目的长度

import random

races = ['Dragonborn', 'Dwarf', 'Elf', 'Gnome', 'Half-Elf', 'Halfling', 'Half-Orc', 'Human', 'Tiefling']

races_choice = random.choice(races)

subraces = {
'Dwarf': ['Hill Dwarf', 'Mountain Dwarf'],
'Elf': ['High Elf', 'Wood Elf', 'Dark Elf'],
'Halfling': ['Lightfoot', 'Stout'],
'Gnome': ['Forest Gnome', 'Rock Gnome']}

if races_choice in subraces:
    print(subraces[races_choice][random.randint(0, len(subraces[races_choice]) - 1)])  # -1 since lists starts from 0, not 1
else:
    print('"' + races_choice + '"', 'has no subraces')

you also can try something like this:你也可以尝试这样的事情:

from random import choice

print(choice(subraces.get(choice(races),[0])) or 'no matches')

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

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