从字典中的键获取全部价值时遇到问题

时间:2018-08-04 05:07:58

标签: python dictionary

我在字典中存储了3个值:

gunStats = ("M4", 30, "AR")
PrimaryGun["Primary"] = gunStats

这非常好用,但是现在我正试图通过执行以下操作来访问存储的所有3个统计信息:

for values in PrimaryGun:
    statsOfGun = values
OldPrimaryGun["Old Primary"] = values

这仅返回统计信息之一。

如果还有更好的方法提示,请帮帮我,它通过使用另一本词典中的值并将其输入到另一本词典中来装备枪支!

2 个答案:

答案 0 :(得分:1)

您正在遍历键,而不是值。在您的代码中,for values in PrimaryGun:遍历键,例如"Primary",而不是统计信息。为此,您需要遍历PrimaryGun.values()。这里有一些代码可能有助于弄清楚。

guns = [("M4", 30, "AR"),
               ("M14", 20, "SA"),
               ("M16", 20, "CM"),
               ("AR15", 20, "AR")]
positions = ["Primary",
             "Secondary",
             "Tertiary",
             "Quaternary"]
gunStats = dict((zip(positions, guns)))

print(gunStats)

# returns:
# {'Primary': ('M4', 30, 'AR'),
# 'Secondary': ('M14', 20, 'SA'),
# 'Tertiary': ('M16', 20, 'CM'),
# 'Quaternary': ('AR15', 20, 'AR')}

for gun_stat in gunStats.values():
    statsOfGun = gun_stat
    print(statsOfGun)

# returns:
# ('M4', 30, 'AR')
# ('M14', 20, 'SA')
# ('M16', 20, 'CM')
# ('AR15', 20, 'AR')

for gun_stat in gunStats.values():
    rifle, rounds, manufacturer = gun_stat
    print(f'The {rifle} holds {rounds} rounds and its manufacturer initials are {manufacturer}')

# returns:
# the M4 holds 30 rounds and its manufacturer initials are AR
# the M14 holds 20 rounds and its manufacturer initials are SA
# the M16 holds 20 rounds and its manufacturer initials are CM
# the AR15 holds 20 rounds and its manufacturer initials are AR

for gun_info in gunStats.items():
    position, gun_stat = gun_info
    rifle, rounds, manufacturer = gun_stat
    print(f'The {rifle} holds {rounds} rounds, its manufacturer initials are {manufacturer}, '
          f'and it is the {position} gun in my collection.')

# returns:
# The M4 holds 30 rounds, its manufacturer initials are AR, and it is the Primary gun in my collection.
# The M14 holds 20 rounds, its manufacturer initials are SA, and it is the Secondary gun in my collection.
# The M16 holds 20 rounds, its manufacturer initials are CM, and it is the Tertiary gun in my collection.
# The AR15 holds 20 rounds, its manufacturer initials are AR, and it is the Quaternary gun in my collection.

在这种情况下,我可能会使用namedtuples来避免为了使用它们而必须解压缩值。

from collections import namedtuple

GunStat = namedtuple('GunStat', 'rifle, rounds, manufacturer')

guns = [GunStat("M4", 30, "AR"),
        GunStat("M14", 20, "SA"),
        GunStat("M16", 20, "CM"),
        GunStat("AR15", 20, "AR")]
positions = ["Primary",
             "Secondary",
             "Tertiary",
             "Quaternary"]
gunStats = dict((zip(positions, guns)))

print(gunStats)

# returns:
# {'Primary': GunStat(rifle='M4', rounds=30, manufacturer='AR'),
# 'Secondary': GunStat(rifle='M14', rounds=20, manufacturer='SA'),
# 'Tertiary': GunStat(rifle='M16', rounds=20, manufacturer='CM'),
# 'Quaternary': GunStat(rifle='AR15', rounds=20, manufacturer='AR')}


for gun_stat in gunStats.values():
    statsOfGun = gun_stat
    print(statsOfGun)

# returns:
# GunStat(rifle='M4', rounds=30, manufacturer='AR')
# GunStat(rifle='M14', rounds=20, manufacturer='SA')
# GunStat(rifle='M16', rounds=20, manufacturer='CM')
# GunStat(rifle='AR15', rounds=20, manufacturer='AR')

for gun_stat in gunStats.values():
    print(f'The {gun_stat.rifle} holds {gun_stat.rounds} rounds and its '
          f'manufacturer initials are {gun_stat.manufacturer}')

# returns:
# the M4 holds 30 rounds and its manufacturer initials are AR
# the M14 holds 20 rounds and its manufacturer initials are SA
# the M16 holds 20 rounds and its manufacturer initials are CM
# the AR15 holds 20 rounds and its manufacturer initials are AR

答案 1 :(得分:-1)

当您遍历像这样的字典时

for x in Dict

您正在遍历字典中的所有。要遍历字典的值,请像下面这样在Dict.values()上调用for循环:

for x in Dict.values():
    do_stuff()

所以您的代码变成了

for values in PrimaryGun.values():
    statsOfGun = values