累积tupelet列表中的值[python]

时间:2018-06-09 01:43:23

标签: python list sorting dictionary

我有一个通过查询数据库生成的以下tupelet(所有格式相同)的列表:

('topic1', 'Will', 5),
('topic1', 'John', 120),
('topic1', 'John', 15),
('topic8378', 'John', 1),
('topic8378', 'Will', 10),
('topic8378', 'John', 10),
('topic8378', 'Jessica', 5),
('topic8378', 'Will', 1),
('topic15', 'Will', 1),
('topic15', 'John', 1),
('topic15', 'Will', 1),
('topic15', 'Will', 1),
('topic15', 'John', 1),
('topic15', 'John', 1),
('blah10', 'Jessica', 8),
('blah10', 'John', 18),

这些词典的第三个值是“数量”字段。我想通过为每个主题中的每个人添加总金额来累积新的tupelet列表,以便它(例如)返回每个主题的简化总列表(在列表中反向排序):

('topic1', 'John', 135)
('topic1', 'Will', 10)
('topic8378', 'John', 11)
('topic8378', 'Will', 10)
('topic8378', 'Jessica', 5)
('topic15', 'Will', 3)
('topic15', 'John', 3)
('blah10', 'John', 18)
('blah10', 'Jessica, 8)

我应该在这种情况下使用itemgetter吗?交叉检查重复项并累积中间字段总数的最佳方法是什么?

0 个答案:

没有答案
相关问题