我有一个看起来像一个波纹管的列表,有一对重复相同的项目.
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的新手,非常感谢有关如何解决这个问题的任何提示,或者参考哪里可以找到允许我这样做的信息.谢谢!