Python字典键对

3phpmpom  于 2023-11-20  发布在  Python
关注(0)|答案(5)|浏览(140)

我有一个字典,它是这样格式化的:

enemyType = {"skeleton": [{"HP": random.randint(12,15), "DMG": random.randint(3,7)}],
        "zombie": [{"HP": random.randint(21,33), "DMG": random.randint(5,9)}],
        "golem": [{"HP": random.randint(40,55), "DMG": random.randint(6,13)}]}

    for key, value in enemyType.items():
     print(key, value)
     for k, v in value:
       print(k)

字符串
输出为:

skeleton [{'HP': 14, 'DMG': 3}]
    HP
    zombie [{'HP': 28, 'DMG': 8}]
    HP
    golem [{'HP': 45, 'DMG': 8}]
    HP


如何获取“HP”和“DMG”的值?
谢谢你

gcxthw6b

gcxthw6b1#

字典的值->包含一个项目的列表->键和值:

import random

enemyType = {
    'skeleton': [{'HP': random.randint(12, 15), 'DMG': random.randint(3, 7)}],
    'zombie': [{'HP': random.randint(21, 33), 'DMG': random.randint(5, 9)}],
    'golem': [{'HP': random.randint(40, 55), 'DMG': random.randint(6, 13)}],
}

for k, v in enemyType.items():
    d = v[0]
    print(k, d['HP'], d['DMG'])

字符串
产出:

skeleton 14 3
zombie 21 9
golem 40 11


我想你可以删除列表,这样你的结构就像:

enemyType = {
    'skeleton': {'HP': random.randint(12, 15), 'DMG': random.randint(3, 7)},
    'zombie': {'HP': random.randint(21, 33), 'DMG': random.randint(5, 9)},
    'golem': {'HP': random.randint(40, 55), 'DMG': random.randint(6, 13)},
}

8qgya5xd

8qgya5xd2#

首先,你需要纠正for循环中的错误。

import random

enemyType = {
    'skeleton': [{'HP': random.randint(12, 15), 'DMG': random.randint(3, 7)}],
    'zombie': [{'HP': random.randint(21, 33), 'DMG': random.randint(5, 9)}],
    'golem': [{'HP': random.randint(40, 55), 'DMG': random.randint(6, 13)}],
}

[(k,v[0]['HP'],v[0]['DMG']) for k, v in enemyType.items()]

#output
[('skeleton', 15, 3), ('zombie', 28, 6), ('golem', 41, 7)]

字符串
如果你只需要'HP''DMG',那么:

[(v[0]['HP'],v[0]['DMG']) for k, v in enemyType.items()]

#output
[(15, 3), (28, 6), (41, 7)]


如果你想从你的字典中删除列表,只需要:

enemyType = {k:v[0] for k,v in enemyType.items()}

#output
{'skeleton': {'HP': 15, 'DMG': 3},
 'zombie': {'HP': 28, 'DMG': 6},
 'golem': {'HP': 41, 'DMG': 7}}
for key, value in enemyType.items():
    for k,v in value.items():
        print(k,v)

HP 15
DMG 3
HP 28
DMG 6
HP 41
DMG 7

的字符串

50pmv0ei

50pmv0ei3#

您可以通过迭代字典,然后访问列表中嵌套字典的键来访问HPDMG的值。类似于:

for k, v in enemyType.items():
    print(k, v)
    for enemy in v:
        hp = enemy['HP']
        dmg = enemy['DMG']
        print("HP:", hp, "DMG:", dmg)

字符串
输出量:

skeleton [{'HP': 12, 'DMG': 3}]
HP: 12 DMG: 3
zombie [{'HP': 33, 'DMG': 7}]
HP: 33 DMG: 7
golem [{'HP': 45, 'DMG': 6}]
HP: 45 DMG: 6

zdwk9cvp

zdwk9cvp4#

  • "...如何获取'HP'和'DMG'的值?..."*

这些值在一个 list 中,所以你需要先指定这个索引。

import random as r
d = {'skeleton': [{'HP': r.randint(12,15), 'DMG': r.randint(3,7)}],
     'zombie':   [{'HP': r.randint(21,33), 'DMG': r.randint(5,9)}],
     'golem':    [{'HP': r.randint(40,55), 'DMG': r.randint(6,13)}]}
for k, v in d.items():
    hp, dmg = v[0]['HP'], v[0]['DMG']
    print(k, hp, dmg)

字符串
可选地,仅使用 * 字典 *。

import random as r
d = {'skeleton': {'HP': r.randint(12,15), 'DMG': r.randint(3,7)},
     'zombie':   {'HP': r.randint(21,33), 'DMG': r.randint(5,9)},
     'golem':    {'HP': r.randint(40,55), 'DMG': r.randint(6,13)}}
for k, v in d.items():
    hp, dmg = v['HP'], v['DMG']
    print(k, hp, dmg)


输出

skeleton 14 4
zombie 28 8
golem 54 11

uoifb46i

uoifb46i5#

下面是在你现有的代码中发生的事情:基本上你不小心格式化了你的数据结构和你的for循环,而这种方式并没有导致显式错误,这通常是你知道有什么问题的方式。

for key, value in enemyDict.items():
    print(key, value) #here value is a list containing a single dict
    
    for k, v in value: #Iterating over the list yields a single iteration over this loop
        #For each loop iteration you are trying to unpack the new value into two new variables.
        #  This only works here because your dict (the first and only value in the list)
        #  also only has two keys. Treating the dict as an iterable (without calling .items
        #  or .values for example) will simply iterate over the keys
       print(k) #here k will be the first key in the dict: "HP", and v will be the second: "DMG".

字符串
如果你不想拥有额外的列表,那么删除它很简单:

enemyType = {
    "skeleton": {"HP": random.randint(12,15), "DMG": random.randint(3,7)},
    "zombie": {"HP": random.randint(21,33), "DMG": random.randint(5,9)},
    "golem": {"HP": random.randint(40,55), "DMG": random.randint(6,13)}
}

for key, value in enemyDict.items():
    print(key, value)
    for k, v in value.items(): #note the added "items" to get key and value pairs from the dict
        print(k, v)


如果你确实想让列表出现在那里(例如,如果你试图表示每个敌人的多个示例),你也可以相应地调整循环:

enemyType = {
    #using list comprehensions to create multiple enemies of each type
    "skeleton": [{"HP": random.randint(12,15), "DMG": random.randint(3,7)} for _ in range(3)],
    "zombie": [{"HP": random.randint(21,33), "DMG": random.randint(5,9)} for _ in range(4)],
    "golem": [{"HP": random.randint(40,55), "DMG": random.randint(6,13)} for _ in range(2)]
}

for enemy_type, instances in enemyDict.items(): #get enemy type and list of enemy instances of that type
    print(enemy_type)
    for enemy_instance in instances: #each instance is a dict containing specific "HP" and "DMG" for each enemy
        print(enemy_instance["HP"], enemy_instance["DMG"])

相关问题