在python词典中查找术语

2024-05-29 02:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

如果我有口袋妖怪的名单:

not_e =     {"Bulbasaur": ["Overgrow", "Tackle", "Leech Seed"],
                  "Charmander": ["Blaze", "Scratchy", "Growl"],
                  "Squirtle": ["Torrent", "Tackle", "Tail Whip"],
                  "Pikachu": ["Static", "Tail Whip", "Thunder Shock"],
                  "Haunter": ["Levitate", "Hypnosis", "Spite"],}

如果我想随机选择一个口袋妖怪,我是这样做的:

random_pokemon = ["Bulbasaur", "Charmander", "Squirtle", "Pikachu", "Haunter"] 
rand_pokemon = random.choice(random_pokemon) 
print("Your pokemon is: " + rand_pokemon)

我该如何在字典中搜索随机的口袋妖怪,并从中获取异能列表?你知道吗


Tags: notrandomtailpokemon名单whiprand口袋妖怪
2条回答

如果要以更方便用户的格式打印电源列表,请使用:

print ' '.join(not_e[character])

这将打印所有由空格分隔的幂。如果你想用新线把它们分开,用

print '\n'.join(not_e[character])
# this randomly selects the character.
character = random.choice(not_e.keys())

# this prints the powers of the randomly selected characters.
print not_e[character]

如果你想要的只是随机选择的pokemon角色的能力,你可以把这两个语句组合起来,就像这样

print not_e[random.choice(not_e.keys())]

相关问题 更多 >

    热门问题