2014-03-02 184 views
0

说短期,如果我有宠物小精灵列表:查找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) 

会如何我在那本词典中搜索随机口袋妖怪,并从中获得权力列表?

+1

为什么'not_e [rand_pokemon]'? – ton1c

回答

2
# 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())] 
+0

我认为这将打印一个列表,他可能想要一个更方便用户的输出 – Dunno

+0

@Dunno但问题是“从列表中获取权力列表”,对吧? – thefourtheye

+0

没错,只是说。也许我会发布一个替代答案 – Dunno

0

如果你要打印在多个用户权力清单友好的格式使用:

print ' '.join(not_e[character]) 

这将打印由空格分隔的所有权力。如果你想用新行分隔它们,请使用

print '\n'.join(not_e[character])