如何从下面的dict列表中删除重复项,同时总结相应的值谢谢
{'VIC': [['City West ', 10.0], ['City West ', 30.0], ['Thornbury ',
0.0]], 'NSW': [['Queanbeyan ', 5.0],['Queanbeyan ', 20.0]],
'ACT': [['Civic ', 8.0], ['Civic ', 60.0], ['Civic ', 1.0]]}
期望的输出
{'VIC': [['City West ', 40.0], ['Thornbury ', 0.0]], 'NSW':
[['Queanbeyan ', 25.0]], 'ACT': [['Civic ', 69.0]]}
答案 0 :(得分:4)
您可以使用dict-comprehension重建dict
。在重建期间,您可以使用itertools.groupby
将嵌套列表分组到每个子列表的第一个值上,并使用列表推导对第二个值求和:
>>> from itertools import groupby
>>> d = {'VIC': [['City West ', 10.0], ['City West ', 30.0], ['Thornbury ', 0.0]], 'NSW': [['Queanbeyan ', 5.0],['Queanbeyan ', 20.0]], 'ACT': [['Civic ', 8.0], ['Civic ', 60.0], ['Civic ', 1.0]]}
>>> {k:[[ke,sum([i[1] for i in list(g)])] for ke,g in groupby(sorted(v), key= lambda x: x[0])] for k,v in d.items()}
>>> {'NSW': [['Queanbeyan ', 25.0]], 'VIC': [['City West ', 40.0], ['Thornbury ', 0.0]], 'ACT': [['Civic ', 69.0]]}
也可以使用operator.itemgetter
代替lambda
重写上述表达式,以获得一些性能
>>> from operator import itemgetter
>>> {k:[[ke,sum([i[1] for i in list(g)])] for ke,g in groupby(sorted(v), key=itemgetter(0))] for k,v in d.items()}
答案 1 :(得分:0)
您可以使用以下
获得所需的输出dictionary = {'VIC': [['City West ', 10.0], ['City West ', 30.0], ['Thornbury ', 0.0]], 'NSW': [['Queanbeyan ', 5.0],['Queanbeyan ', 20.0]], 'ACT': [['Civic ', 8.0], ['Civic ', 60.0], ['Civic ', 1.0]]}
dic = {}
for i in dictionary:
temp = {}
for j in dictionary[i]:
if not j[0] in temp: temp.update({j[0]: j[1]})
else: temp[j[0]] += j[1]
dic.update({i: [[j, temp[j]] for j in temp]})
dic
{' ACT':[[' Civic',69.0]],' NSW':[[' Queanbeyan& #39;,25.0]],' VIC': [[' City West',40.0],[' Thornbury',0.0]]}
但是我会建议你唱一本字典词典以简化你可以做以下事情
dictionary = {'VIC': [['City West ', 10.0], ['City West ', 30.0], ['Thornbury ', 0.0]], 'NSW': [['Queanbeyan ', 5.0],['Queanbeyan ', 20.0]], 'ACT': [['Civic ', 8.0], ['Civic ', 60.0], ['Civic ', 1.0]]}
dic = {}
for i in dictionary:
temp = {}
for j in dictionary[i]:
if not j[0] in temp: temp.update({j[0]: j[1]})
else: temp[j[0]] += j[1]
dic.update({i: temp})
print(dic)
{' ACT':{' Civic':69.0},' NSW':{' Queanbeyan' :25.0},' VIC': {' City West':40.0,' Thornbury':0.0}}
答案 2 :(得分:0)
使用defaultdict
将我的帽子扔进戒指In [6]: from collections import defaultdict
In [7]: tree = lambda: defaultdict(tree)
In [8]: a
Out[8]:
{'ACT': [['Civic ', 8.0], ['Civic ', 60.0], ['Civic ', 1.0]],
'NSW': [['Queanbeyan ', 5.0], ['Queanbeyan ', 20.0]],
'VIC': [['City West ', 10.0], ['City West ', 30.0], ['Thornbury ', 0.0]]}
In [9]: b = tree()
In [10]: for key in a:
...: for entry in a[key]:
...: if b[key][entry[0]]:
...: b[key][entry[0]] += entry[1]
...: else:
...: b[key][entry[0]] = entry[1]
...:
In [11]: b
Out[11]:
defaultdict(<function __main__.<lambda>>,
{'ACT': defaultdict(<function __main__.<lambda>>,
{'Civic ': 69.0}),
'NSW': defaultdict(<function __main__.<lambda>>,
{'Queanbeyan ': 25.0}),
'VIC': defaultdict(<function __main__.<lambda>>,
{'City West ': 40.0, 'Thornbury ': 0.0})})
In [12]: c = {}
In [14]: for key in b:
...: for entry in b[key]:
...: if key in c:
...: c[key].append([entry, b[key][entry]])
...: else:
...: c[key] = [[entry, b[key][entry]]]
...:
In [15]: c
Out[15]:
{'ACT': [['Civic ', 69.0]],
'NSW': [['Queanbeyan ', 25.0]],
'VIC': [['City West ', 40.0], ['Thornbury ', 0.0]]}
我使用了一个活泼的哈希映射来执行此操作,然后使用defaultdict
。我喜欢它创建的自动分组功能。