将列表中的重复项合并到python字典中

时间:2013-11-18 16:08:27

标签: python list python-3.x dictionary

我有一个看起来像下面一个列表的列表,其中一对的项目重复了一次。

l = (['aaron distilled ', 'alcohol', '5'], 
['aaron distilled ', 'gin', '2'], 
['aaron distilled ', 'beer', '6'], 
['aaron distilled ', 'vodka', '9'], 
['aaron evicted ', 'owner', '1'], 
['aaron evicted ', 'bum', '1'], 
['aaron evicted ', 'deadbeat', '1'])

我想将它转换为字典列表,在其中我将第一项的所有重复合并为一个键,因此最终结果如下:

data = {'aaron distilled' :  ['alcohol', '5', 'gin', '2',  'beer', '6', 'vodka', '9'], 
'aaron evicted ':  ['owner', '1', 'bum', '1', 'deadbeat', '1']}

我正在尝试类似的事情:

result = {}
for row in data:
    key = row[0]
    result = {row[0]: row[1:] for row in data}

for dicts in data:
   for key, value in dicts.items():
    new_dict.setdefault(key,[]).extend(value)

但是我得到了错误的结果。我是python的新手,非常感谢有关如何解决这个问题的任何提示,或者参考哪里可以找到允许我这样做的信息。谢谢!

2 个答案:

答案 0 :(得分:6)

轻松使用collections.defaultdict() object

from collections import defaultdict

result = defaultdict(list)

for key, *values in data:
    result[key].extend(values)

您的第一次尝试将覆盖密钥;字典理解不会合并价值观。第二次尝试似乎将data列表中的列表视为dictonaries,因此根本不起作用。

演示:

>>> from collections import defaultdict
>>> data = (['aaron distilled ', 'alcohol', '5'], 
... ['aaron distilled ', 'gin', '2'], 
... ['aaron distilled ', 'beer', '6'], 
... ['aaron distilled ', 'vodka', '9'], 
... ['aaron evicted ', 'owner', '1'], 
... ['aaron evicted ', 'bum', '1'], 
... ['aaron evicted ', 'deadbeat', '1'])
>>> result = defaultdict(list)
>>> for key, *values in data:
...    result[key].extend(values)
... 
>>> result
defaultdict(<class 'list'>, {'aaron distilled ': ['alcohol', '5', 'gin', '2', 'beer', '6', 'vodka', '9'], 'aaron evicted ': ['owner', '1', 'bum', '1', 'deadbeat', '1']})

答案 1 :(得分:1)

如果L中的项目按第一个元素排序,则可以使用groupby

>>> L = (['aaron distilled ', 'alcohol', '5'], 
... ['aaron distilled ', 'gin', '2'], 
... ['aaron distilled ', 'beer', '6'], 
... ['aaron distilled ', 'vodka', '9'], 
... ['aaron evicted ', 'owner', '1'], 
... ['aaron evicted ', 'bum', '1'], 
... ['aaron evicted ', 'deadbeat', '1'])
>>> from operator import itemgetter
>>> from itertools import groupby
>>> {k: [j for a,b,c in g for j in b,c] for k, g in groupby(L, itemgetter(0))}
{'aaron evicted ': ['owner', '1', 'bum', '1', 'deadbeat', '1'], 'aaron distilled ': ['alcohol', '5', 'gin', '2', 'beer', '6', 'vodka', '9']}
相关问题