2014-04-03 68 views
-3

我是python編程新手。我正在嘗試製作一個程序,作爲天際藥水效果的互動詞典,以便練習拼寫檢查功能。python dict.keys()不返回所有值

這是我的代碼的相關位。

effects = {'curedisease' : ['Charred Skeever Hide', 'Felsaad Tern Feathers', 'Hawk Feathers', 'Mudcrab Chitin', 'Vampire Dust'], 'damagehealth' : ['Crimson Nirnroot', 'Deathbell', 'Ectoplasm', 'Emperor Parasol Moss', 'Falmer Ear', 'Human Flesh', 'Human Heart', 'Imp Stool', 'Jarrin Root', 'Nightshade', 'Nirnroot', 'Poison Bloom', 'Red Mountain Flower', 'River Betty', 'Skeever Tail', 'Small Antlers', 'Troll Fat', 'Void Salts'], 'damagemagicka' : ['Butterfly Wing',' Chaurus Eggs', 'Daedra Heart', 'Eye of Sabre Cat', 'Glow Dust', 'Hagraven Feathers', 'Hanging Moss', 'Human Heart', 'Jarrin Root', 'Luna Moth Wing', "Namira's Rot", 'Nordic Barnacle', 'Trama Root'], 'damageMagikaregen' : ['Bear Claws', 'Blue Butterfly Wing', 'Blue Mountain Flower', 'Chicken\'s Egg', 'Glow Dust', 'Hanging Moss', 'Human Heart', 'Jarrin Root', 'Nightshade', 'Spider Egg', 'Spriggan Sap'], 'damagestamina' : ['Blisterwort', 'Blue Butterfly Wing', 'Bone Meal', 'Canis Root', 'Crimson Nirnroot', 'Cyrodilic Spadetail', 'Giant\'s Toe', 'Jarrin Root', 'Nirnroot', 'Rock Warbler Egg', 'Spider Egg'], 'damagestaminasegen' : ['Creep Cluster', 'Daedra Heart', 'Frost Mirriam', 'Giant\'s Toe', 'Histcarp', 'Juniper Berries', 'Large Antlers', 'Silverside Perch', 'Skeever Tail', 'Wheat'], 'fear' : ['Blue Dartwing', 'Cyrodilic Spadetail', 'Daedra Heart', 'Namira\'s Rot', 'Powdered Mammoth Tusk'], 'fortifyalteration' : ['Grass Pod', 'River Betty', 'Spriggan Sap'], 'fortifybarter' : ['Butterfly Wing', 'Dragon\'s Tongue', 'Hagraven Claw', 'Tundra Cotton'], 'fortifyblock' : ['Bleeding Crown', 'Briar Heart', 'Honeycomb', 'Pearl', 'Slaughterfish Scales', 'Tundra Cotton'], 'fortifycarryWeight' : ['Creep Cluster', 'Giant\'s Toe', 'Hawk Beak', 'River Betty', 'Scaly Pholiata', 'Wisp Wrappings'], 'fortifyconjuration' : ['Blue Butterfly Wing', 'Blue Mountain Flower', 'Bone Meal', 'Frost Salts', 'Hagraven Feathers', 'Lavender'], 'fortifydestruction' : ['Beehive Husk', 'Ectoplasm', 'Glow Dust', 'Glowing Mushroom', 'Nightshade', 'Wisp Wrappings'], 'fortifyenchanting' : ['Blue Butterfly Wing', 'Hagraven Claw', 'Snowberries', 'Spriggan Sap'], 'fortifyhealth' : ['Bear Claws', 'Blue Mountain Flower', 'Giant\'s Toe', 'Glowing Mushroom', 'Hanging Moss', 'Wheat'], 'fortifyheavyarmor' : ['Ice Wraith Teeth', 'Sabre Cat Tooth', 'Slaughterfish Scales', 'Thistle Branch', 'White Cap'], 'fortifyillusion' : ['Dragon\'s Tongue', 'Dwarven Oil', 'Mora Tapinella', 'Scaly Pholiata', 'Taproot'], 'fortifylightarmor' : ['Ice Wraith Teeth', 'Sabre Cat Tooth', 'Slaughterfish Scales', 'Thistle Branch', 'White Cap'], 'fortifylockpicking' : ['Falmer Ear', 'Namira\'s Rot', 'Pine Thrush Egg', 'Spider Egg'], 'fortifymagicka ' : ['Briar Heart', 'Ectoplasm', 'Histcarp', 'Jazbay Grapes', 'Red Mountain Flower', 'Tundra Cotton', 'Void Salts'], 'fortifymarksman' : ['Canis Root', 'Elves Ear', 'Juniper Berries', 'Spider Egg'], 'fortifyonehanded' : ['Bear Claws', 'Canis Root', 'Hanging Moss', 'Hawk Feathers', 'Rock Warbler Egg', 'Small Pearl'], 'fortifypickpocket' : ['Blue Dartwing', 'Nordic Barnacle', 'Orange Dartwing', 'Slaughterfish Egg'], 'fortifyrestoration' : ['Abecean Longfin', 'Cyrodilic Spadetail', 'Salt Pile', 'Small Antlers', 'Small Pearl'], 'fortifysmithing' : ['Blisterwort', 'Glowing Mushroom', 'Sabre Cat Tooth', 'Spriggan Sap'], 'fortifysneak' : ['Abecean Longfin', 'Beehive Husk', 'Frost Mirriam', 'Hawk Feathers', 'Human Flesh', 'Powdered Mammoth Tusk', 'Purple Mountain Flower'], 'fortifystamina' : ['Chaurus Eggs', 'Garlic', 'Large Antlers', 'Lavender', 'Slaughterfish Egg', 'Torchbug Thorax'], 'lingeringdamagehealth' : ['Imp Stool', 'Mora Tapinella', 'Orange Dartwing', 'Slaughterfish Egg', 'Slaughterfish Scales'], 'lingeringdamagemagicka' : ['Hagraven Claw', 'Purple Mountain Flower', 'Swamp Fungal Pod', 'Torchbug Thorax', 'Wheat'], 'lingeringdamagestamina' : ['Butterfly Wing', 'Chicken\'s Egg', 'Nightshade', 'Small Antlers'], 'paralysis' : ['Briar Heart', 'Canis Root', 'Human Flesh', 'Imp Stool', 'Swamp Fungal Pod'], 'ravagehealth' : ['Cyrodilic Spadetail', 'Eye of Sabre Cat', 'Giant Lichen', 'Jazbay Grapes', 'Silverside Perch', 'Skeever Tail'], 'ravagemagicka' : ['Frost Mirriam', 'Grass Pod', 'Lavender', 'Orange Dartwing', 'Red Mountain Flower', 'White Cap'], 'ravagestamina' : ['Bee', 'Bone Meal', 'Deathbell', 'Honeycomb', 'Thistle Branch'], 'regeneratehealth' : ['Garlic', 'Juniper Berries', 'Luna Moth Wing', 'Namira\'s Rot', 'Nordic Barnacle', 'Vampire Dust'], 'regeneratemagicka' : ['Dwarven Oil', 'Fire Salts', 'Garlic', 'Jazbay Grapes', 'Moon Sugar', 'Salt Pile', 'Taproot'], 'regeneratestamina' : ['Bee', 'Fly Amanita', 'Mora Tapinella', 'Scaly Pholiata'], 'resistfire' : ['Bone Meal', 'Dragon\'s Tongue', 'Elves Ear', 'Fire Salts', 'Fly Amanita', 'Mudcrab Chitin', 'Snowberries'], 'resistfrost' : ['Frost Mirriam', 'Frost Salts', 'Hawk Beak', 'Moon Sugar', 'Purple Mountain Flower', 'Silverside Perch', 'Slaughterfish Scales', 'Small Pearl', 'Snowberries', 'Thistle Branch'], 'resistmagic' : ['Bleeding Crown', 'Chicken\'s Egg', 'Crimson Nirnroot', 'Hagraven Claw', 'Lavender', 'Nirnroot', 'Tundra Cotton', 'Void Salts', 'Wisp Wrappings'], 'resistpoison' : ['Beehive Husk', 'Charred Skeever Hide', 'Falmer Ear', 'Garlic', 'Grass Pod', 'Mudcrab Chitin', 'Slaughterfish Egg', 'Thistle Branch', 'Troll Fat'], 'resistshock' : ['Blue Dartwing', 'Glow Dust', 'Glowing Mushroom', 'Hawk Beak', 'Pearl', 'Pine Thrush Egg', 'Snowberries', 'Swamp Fungal Pod'], 'restorehealth' : ['Blisterwort', 'Blue Dartwing', 'Blue Mountain Flower', 'Butterfly Wing', 'Charred Skeever Hide', 'Daedra Heart', 'Eye of Sabre Cat', 'Imp Stool', 'Rock Warbler Egg', 'Swamp Fungal Pod', 'Wheat'], 'restoremagicka ' : ['Briar Heart', 'Creep Cluster', 'Dwarven Oil', 'Ectoplasm', 'Elves Ear', 'Fire Salts', 'Frost Salts', 'Giant Lichen', 'Grass Pod', 'Human Flesh', 'Moon Sugar', 'Mora Tapinella', 'Pearl', 'Red Mountain Flower', 'Taproot', 'Vampire Dust', 'White Cap']} 

effectslist = effects.keys() 
print (effectslist) 

麻煩的是,effectslist將不包含字典中的作用的所有鍵。我不清楚它是否一直放棄相同的鍵或循環列表,但我需要它創建一個用作特效字典中鍵的字符串的完整列表。

任何人都可以向我解釋爲什麼它沒有列出完整的列表,我可以做些什麼來創建完整列表?

謝謝

+1

該代碼絕對沒有錯。它工作得很好。 –

回答

1

麻煩的是,effectslist將不包含字典中的作用的所有鍵。

其實沒有。它包含所有的鍵。

首先,keys()返回正確數量的元素:

>>> len(effects) 
44 
>>> len(effects.keys()) 
44 

其次,所有的元素,它返回是唯一的:

>>> len(set(effects.keys())) 
44 

最後,每次返回鍵出現在詞典:

>>> all(k in effects for k in effects.keys()) 
True 

如果您要嘗試找到一個特定的密鑰,不是由.keys()返回,我相信你會發現沒有一個。

1

我自己運行了代碼,看起來len(effects)==len(effectslist),所以所有帳戶都返回完整列表。是什麼讓你覺得它沒有返回完整列表?請記住,詞典不以固定順序存在 - 因此,當您查詢密鑰時,它們將不會與您輸入的順序相同。