识别列表中的重复项并总结其最后一项 [英] Identify duplicates in a list of lists and sum up their last items

查看:40
本文介绍了识别列表中的重复项并总结其最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表,我希望从这些列表中删除重复项并总结重复项的最后一个元素.如果项目的前两个元素相同,则该项目为重复项.用一个例子可以更好地说明这一点:

I have a list of lists from which I would like to remove duplicates and sum up duplicates' last elements. An item is a duplicate if its first 2 elements are the same. This is better illustrated with an example:

input = [['a', 'b', 2], ['a', 'c', 1], ['a', 'b', 1]]

# Desired output
output = [['a', 'b', 3], ['a', 'c', 1]]

这里也有类似的问题,但是我还没有找到一个可以同时处理列表和汇总列表项的问题.

There are similar questions here on SO but I haven't found one which would deal with list of lists and summing up list items at the same time.

我尝试了几种方法,但无法使其起作用:

I tried several approaches but couldn't make it work:

  • 创建输入列表的副本,进行嵌套循环,如果找到第二个重复项,则将其最后一项添加到原始项目中->这样会与太多嵌套混淆不清
  • 我查看了Counter集合,但它似乎不适用于列表列表
  • itertools

您能给我一些有关如何解决此问题的建议吗?

Could you give me any pointers on how to approach this problem?

推荐答案

我认为列表不是最佳的数据结构.我会用带元组键的字典.我确实需要列表,以后可以创建一个列表:

I don't think lists are the best data structure for it. I would use dictionaries with tuple key. I you really need list, you can create one later:

from collections import defaultdict

data = [['a', 'b', 2], ['a', 'c', 1], ['a', 'b', 1]]

result = collections.defaultdict(int) # new keys are auto-added and initialized as 0
for item in data:
    a, b, value = item
    result[(a,b)] += value
print result
# defaultdict(<type 'int'>, {('a', 'b'): 3, ('a', 'c'): 1})
print dict(result)
# {('a', 'b'): 3, ('a', 'c'): 1}
print [[a, b, total] for (a, b), total in result.items()]
# [['a', 'b', 3], ['a', 'c', 1]]

这篇关于识别列表中的重复项并总结其最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆