访问键内的 index/value (Python)

Accessing an index/value inside a key (Python)

我有一个字典,有 18 个不同的键,每个键都有 3 个值(xPowerxPPxAccuracy),每个变量都有一个 int。我如何从某个键访问一个值?比如我想把ScratchPower(40)的值取出来用在别处。搜索了半个多小时后,我发现最多的是从一个键访问所有值:

print defMoves["Scratch"]

这是我的词典:

defMoves = {
    "Scratch": [ScratchPower, ScratchPP, ScratchAccuracy],
    "Air Slash": [Air_SlashPower, Air_SlashPP, Air_SlashAccuracy],
    "Flare Blitz": [Flare_BlitzPower, Flare_BlitzPP, Flare_BlitzAccuracy],
    "Growl": [GrowlPower, GrowlPP, GrowlAccuracy],
    "Heat Wave": [Heat_WavePower, Heat_WavePP, Heat_WaveAccuracy],
    "Ember": [EmberPower, EmberPP, EmberAccuracy],
    "Shadow Claw": [Shadow_ClawPower, Shadow_ClawPP, Shadow_ClawAccuracy],
    "Smokescreen": [SmokescreenPower, SmokescreenPP, SmokescreenAccuracy],
    "Dragon Claw": [Dragon_ClawPower, Dragon_ClawPP, Dragon_ClawAccuracy],
    "Dragon Rage": [Dragon_RagePower, Dragon_RagePP, Dragon_RageAccuracy],
    "Scary Face": [Scary_FacePower, Scary_FacePP, Scary_FaceAccuracy],
    "Fire Fang": [Fire_FangPower, Fire_FangPP, Fire_FangAccuracy],
    "Flame Burst": [Flame_BurstPower, Flame_BurstPP, Flame_BurstAccuracy],
    "Wing Attack": [Wing_AttackPower, Wing_AttackPP, Wing_AttackAccuracy],
    "Slash": [SlashPower, SlashPP, SlashAccuracy],
    "Flamethrower": [FlamethrowerPower, FlamethrowerPP, FlamethrowerAccuracy],
    "Fire Spin": [Fire_SpinPower, Fire_SpinPP, Fire_SpinAccuracy],
    "Inferno": [InfernoPower, InfernoPP, InfernoAccuracy],
}

谢谢

defMoves["Scratch"] returns 一个列表,因此就像您对任何列表进行索引一样:

defMoves["Scratch"][0]  # first subelement -> ScratchPower
defMoves["Scratch"][1]  # second subelement -> ScratchPP
defMoves["Scratch"][2]  # third subelement -> ScratchAccuracy
......

defMoves["Scratch"] 让您返回与该键关联的值,在本例中是一个列表。要从该列表中获取特定项目,您需要使用数字索引。因此,例如,要获得 ScratchPower,您可以使用 defMoves["Scratch"][0]。

不过,这似乎很难跟踪,所以您可能想在这些词典中的每一个中使用另一个词典。看起来像

{"Scratch" : {"Power":40... }... }