将具有相同键字符串的多个字典键值加在一起

时间:2018-10-25 07:11:46

标签: python python-2.7 dictionary

比方说我的字典看起来像这样:

{
    "432701228292636694" : {
        "64AEdcb" : {
            "inviter" : "282764296075870208",
            "uses" : 40
        },
        "9WVmh3Q" : {
            "inviter" : "173219378492473345",
            "uses" : 26
        },
        "AE8ugEN" : {
            "inviter" : "285548207189000204",
            "uses" : 4
        },
        "EmCddbN" : {
            "inviter" : "193926566881918978",
            "uses" : 2
        },
        "GKnPWyW" : {
            "inviter" : "147756076299845632",
            "uses" : 1
        },
        "JnmyFKE" : {
            "inviter" : "173219378492473345",
            "uses" : 10
        },
        "Km4wZTz" : {
            "inviter" : "161132853256650752",
            "uses" : 9
        },
        "M8uYMNb" : {
            "inviter" : "193896125969858561",
            "uses" : 8
        },
        "e9vTXcz" : {
            "inviter" : "273852837988007936",
            "uses" : 1
        },
        "gTB7w4R" : {
            "inviter" : "327660257423917066",
            "uses" : 1
        },
        "hG2KyZa" : {
            "inviter" : "315630485114191872",
            "uses" : 2
        },
        "pdZNPFa" : {
            "inviter" : "141431182792458241",
            "uses" : 537
        },
        "rZwH4QF" : {
            "inviter" : "384664458628169728",
            "uses" : 4
        },
        "s9FXVAU" : {
            "inviter" : "125092433657593856",
            "uses" : 2
        },
        "wrPbkGz" : {
            "inviter" : "277605262486011904",
            "uses" : 3
        }
    }
}

我想将每个具有相同inviter的键的总数相加,以得到总计uses的总数。在上面的数据中,inviter“ 173219378492473345”键分别使用26和10。我希望能够将其组合为36。

我该如何实现?我能够编写一个列表理解语句,该语句可以让我了解每个变量的值,但是将它们加在一起并得出总计值是我迷路的地方。

下面的代码是我已经写的:

codes = sorted(self.invites_db[server.id], key=lambda x: (self.invites_db[server.id][x]['uses']), reverse=True)
inviters = [server.get_member(self.invites_db[server.id][x]['inviter']).name + "#" + server.get_member(self.invites_db[server.id][x]['inviter']).discriminator for x in codes if self.invites_db[server.id][x]['inviter'] != "141431182792458241"]
uses = [self.invites_db[server.id][x]['uses'] for x in codes if self.invites_db[server.id][x]['inviter'] != "141431182792458241"]

这将给我所有invitersuses的列表,但是我不想有两个相同的inviter。相反,我希望将它们作为一个整体添加。我该怎么做,以便他们可以使用zip以文本格式并排显示此数据?

3 个答案:

答案 0 :(得分:2)

collections.Counter用于那些“算我或零”的东西/累积问题(您无法使用列表推导来解决):

result = collections.Counter()
for subdict in d["432701228292636694"].values():
    result[subdict["inviter"]] += subdict["uses"]

结果:

>>> result
{'125092433657593856': 2,
 '141431182792458241': 537,
 '147756076299845632': 1,
 '161132853256650752': 9,
 '173219378492473345': 36,
 '193896125969858561': 8,
 '193926566881918978': 2,
 '273852837988007936': 1,
 '277605262486011904': 3,
 '282764296075870208': 40,
 '285548207189000204': 4,
 '315630485114191872': 2,
 '327660257423917066': 1,
 '384664458628169728': 4}

答案 1 :(得分:0)

使用简单的词典难道不是个好时机吗?

uses = {}
for _,v in test["432701228292636694"].items():
    if v["inviter"] in uses:
        uses[v["inviter"]] += v["uses"]
    else:
        uses[v["inviter"]] = v["uses"]

print (uses)

{'282764296075870208': 40, '173219378492473345': 36, '285548207189000204': 4, '193926566881918978': 2, '147756076299845632': 1, '161132853256650752': 9, '193896125969858561': 8, '273852837988007936': 1, '327660257423917066': 1, '315630485114191872': 2, '141431182792458241': 537, '384664458628169728': 4, '125092433657593856': 2, '277605262486011904': 3}

答案 2 :(得分:0)

假设您的字典存储为变量d

o = {}
for s in d.values():
    for i in s.values():
        o[i['inviter']] = o.get(i['inviter'], 0) + i['uses']

o将变为:

{'125092433657593856': 2,
 '141431182792458241': 537,
 '147756076299845632': 1,
 '161132853256650752': 9,
 '173219378492473345': 36,
 '193896125969858561': 8,
 '193926566881918978': 2,
 '273852837988007936': 1,
 '277605262486011904': 3,
 '282764296075870208': 40,
 '285548207189000204': 4,
 '315630485114191872': 2,
 '327660257423917066': 1,
 '384664458628169728': 4}