如果我有一张口袋妖怪的清单:
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)我将如何在字典中搜索随机的口袋妖怪,并从中获取能量列表?
发布于 2014-03-02 14:53:23
# this randomly selects the character.
character = random.choice(not_e.keys())
# this prints the powers of the randomly selected characters.
print not_e[character]如果您想要的只是随机选择的口袋妖怪字符的功能,您可以组合这两个语句,如下所示
print not_e[random.choice(not_e.keys())]发布于 2014-03-02 15:02:55
如果要以更方便用户的格式打印权限列表,请使用:
print ' '.join(not_e[character])这将打印由空格分隔的所有能量。如果要用新行分隔它们,请使用
print '\n'.join(not_e[character])https://stackoverflow.com/questions/22129107
复制相似问题