添加概率并以最高概率获取ID

时间:2018-12-06 13:42:50

标签: python

我具有以下结构:

('2', 0.30334973335266113)
('4', 0.43178531527519226)
('3', 0.3627113997936249)
('9', 0.5691161155700684)
('2', 0.4603477120399475)
('2', 0.7340329885482788)
('10', 0.4691111445426941)
('13', 0.20860238373279572)
('3', 0.4541565775871277)
('2', 0.4479588568210602)
('2', 0.6090611815452576)
('16', 0.5154575705528259)
('11', 0.4370063543319702)
('12', 0.38097500801086426)
('14', 0.23826521635055542)
('3', 0.39956724643707275)
('12', 0.291579008102417)
('11', 0.4514589309692383)

我想获得一个输出,在其中添加每个id的概率,并返回得分最高的那个。

例如3和10:

(3, 1.2)
(10, 0.46)

返回值应为:(3,1.2)

summed = {}
lis = [('2', 0.30334973335266113),
('4', 0.43178531527519226),
('3', 0.3627113997936249),
('9', 0.5691161155700684),
('2', 0.4603477120399475),
('2', 0.7340329885482788),
('10', 0.4691111445426941),
('13', 0.20860238373279572),
('3', 0.4541565775871277),
('2', 0.4479588568210602),
('2', 0.6090611815452576),
('16', 0.5154575705528259),
('11', 0.4370063543319702),
('12', 0.38097500801086426),
('14', 0.23826521635055542),
('3', 0.39956724643707275),
('12', 0.291579008102417),
('11', 0.4514589309692383)]

for i in lis:
    summed[str(i[0])] = i[1]

尽管这会覆盖键,所以只有最后看到的键及其值被存储。插入新键时,我不想覆盖它,我希望将值添加到现有键中。

1 个答案:

答案 0 :(得分:1)

您可以在collections.Counter中收集值,然后获取most_common(1)

>>> lst = [('2', 0.30334973335266113),..., ('11', 0.4514589309692383)]
>>> c = collections.Counter()
>>> for x,y in lst: c[x] += y
>>> c.most_common(1)
[('2', 2.554750472307205)]

关于您现有的代码:您实际上不是求和,而只是用=覆盖以前的值(如果有)。 Intead,您应该使用+=并使用0初始化值(如果尚不存在)。 (collections.Counter会自动执行此操作),然后使用max来获取最大元素。

for x, y in lis:
    if x not in summed:
        summed[x] = 0
    summed[x] += y
print(max(summed.items(), key=lambda t: t[1]))