我有一個列表,例如:將列表轉換爲多值字典
pokemonList = ['Ivysaur', 'Grass', 'Poison', '', 'Venusaur', 'Grass', 'Poison', '', 'Charmander', 'Fire', ''...]
注意,該模式是'Pokemon name', 'its type', ''...next pokemon
口袋妖怪進來單,雙型形式。我該如何編碼,以便每個口袋妖怪(鑰匙)將它們各自的類型應用爲它的值?
到目前爲止,我已經得到了什麼:
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']}
在每個寵物小精靈之後,你總是在列表中有一個空字符串嗎? –
是的。 (謝謝你beautifulsoup)。 – avereux