简体   繁体   English

python dict.keys() 不返回所有值

[英]python dict.keys() not returning all values

I am new to python programming.我是 python 编程的新手。 I am attempting to make a program that serves as an interactive dictionary of skyrim potion effects in order to practice coding spell check functions.我正在尝试制作一个程序,作为天际药水效果的交互式字典,以练习编码拼写检查功能。

The is the relevant bits of my code.这是我的代码的相关位。

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)

The trouble is that effectslist will not contain all keys in the dictionary effects.问题是effectslist 不会包含字典effects 中的所有键。 I am unclear on whether it is leaving out the same keys all the time or cycling the list but I need it to create a complete list of the strings used as keys in the effects dictionary.我不清楚它是一直省略相同的键还是循环列表,但我需要它来创建用作效果字典中键的字符串的完整列表。

Can anybody please explain to me why it is not making a full list and what I can do to create a full list?任何人都可以向我解释为什么它没有制作完整列表以及我可以做些什么来创建完整列表?

Thank you谢谢

The trouble is that effectslist will not contain all keys in the dictionary effects.问题是effectslist 不会包含字典effects 中的所有键。

Actually, no.实际上,没有。 It does contain all the keys.它确实包含所有密钥。

First of all, keys() returns the correct number of elements:首先, keys()返回正确数量的元素:

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

Secondly, all elements it returns are unique:其次,它返回的所有元素都是唯一的:

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

Finally, every returned key is present in the dictionary:最后,每个返回的键都存在于字典中:

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

If you were to try and find a specific key that's not returned by .keys() , I am sure you will discover that there isn't one.如果您尝试查找.keys()未返回的特定密钥,我相信您会发现根本没有。

I ran the code myself and it appears that len(effects)==len(effectslist) , so by all accounts it is returning the full list.我自己运行了代码,看起来len(effects)==len(effectslist) ,因此所有帐户都返回完整列表。 What gives you the idea that it is not returning the full list?是什么让您认为它没有返回完整列表? Remember that dictionaries do not exist in a fixed order - so when you query for the keys they won't be in the same order as you entered them.请记住,字典并不以固定顺序存在 - 因此,当您查询键时,它们的顺序与您输入的顺序不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM