我有一个列表如下:
pokemonList = ['Ivysaur', 'Grass', 'Poison', '', 'Venusaur', 'Grass', 'Poison', '', 'Charmander', 'Fire', ''...]
请注意,模式为
'宝可梦名称','它的类型',''...下一个宝可梦
宝可梦可以是单属性或双属性形式。我该如何编写代码,使每个宝可梦(键)都有其相应的属性作为其值?到目前为止,我已经得到了以下内容:
types = ("", "Grass", "Poison", "Fire", "Flying", "Water", "Bug","Dark","Fighting", "Normal","Ground","Ghost","Steel","Electric","Psychic","Ice","Dragon","Fairy")
pokeDict = {}
for pokemon in pokemonList:
if pokemon not in types:
#the item is a pokemon, append it as a key
else:
for types in pokemonList:
#add the type(s) as a value to the pokemon
正确的字典应该是这样的:
{Ivysaur: ['Grass', 'Poison'], Venusaur['Grass','Poison'], Charmander:['Fire']}