在列表列表中查找重复项 [英] finding duplicates in a list of lists
问题描述
我正在使用Python 2.7,并试图对列表列表进行重复数据删除并合并重复数据的值.
I am using Python 2.7 and am trying to de-duplicate a list of lists and merge the values of the duplicates.
现在我有:
original_list = [['a', 1], ['b', 1], ['a', 1], ['b', 1], ['b', 2], ['c', 2], ['b', 3]]
我想匹配每个嵌套列表的第一个元素,然后添加第二个元素的值.我想以此结束(最终列表的顺序无关紧要):
I want to match on the first element of each nested list and then add the values of the second element. I want to end up with this (the order of the final list does not matter):
ideal_output = [['a', 2], ['b', 7], ['c', 2]]
到目前为止,我有一些代码可以根据每个嵌套列表的第一个元素为我找到重复的值:
So far I have some code that will find me the duplicate values based on the first element of each nested list:
for item in original_list:
matches = -1
for x in original_list:
if (item[0] == x[0]):
matches += 1
if matches >= 1:
if item[0] not in duplicates_list:
duplicates_list.append(item[0])
在这里,我需要搜索original_list中的所有重复项列表,然后将其值相加,但是我不确定执行此操作的最佳方法是什么.
From here I need to search for all duplicates_list items that are in original_list and add up the values, but I am not sure what the best way to do that is.
推荐答案
很多好的答案,但是它们都使用了比我更多的代码,因此,这是我的价值所在:
Lots of good answers, but they all use rather more code than I would for this, so here's my take, for what it's worth:
totals = {}
for k,v in original_list:
totals[k] = totals.get(k,0) + v
# totals = {'a': 2, 'c': 2, 'b': 7}
一旦有了这样的字典,就可以从以下任何答案中使用items
来获取元组列表:
Once you have a dict like that, from any of these answers, you can use items
to get a list of tuples:
totals.items()
# => [('a', 2), ('c', 2), ('b', 7)]
然后在整个元组上映射list
以获得列表列表:
And map list
across the tuples to get a list of lists:
map(list, totals.items())
# => [['a', 2], ['c', 2], ['b', 7]]
并按顺序排序:
sorted(map(list, totals.items()))
# => [['a', 2], ['b', 7], ['c', 2]]
这篇关于在列表列表中查找重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!