如何将相同的字典元素连接在一起python?

时间:2016-11-28 10:16:30

标签: python dictionary

我有一个字典列表,如下所示:

 [{'queryID': '1', 'documentID': '184'}, 
{'queryID': '1', 'documentID': '29'}, 
{'queryID': '1', 'documentID': '31'},
 {'queryID': '1', 'documentID': '12'},
{'queryID': '2', 'documentID': '12'}, 
{'queryID': '2', 'documentID': '15'}, 
{'queryID': '2', 'documentID': '184'}, 
{'queryID': '2', 'documentID': '858'}, 
{'queryID': '2', 'documentID': '51'},
 {'queryID': '2', 'documentID': '102'}]

如何将词典列表更改为

等格式
[{'queryID': '1','documentID':'184','29','31','12'},
{'queryID': '2','documentID':'12','15','184','858','51','102}] 

1 个答案:

答案 0 :(得分:2)

您希望按queryID键对字典列表进行分组,并获取每个组的documentID列表。
在这种情况下,预期输出应包含documentID s的序列使用itertools.groupbyoperator.itemgetter函数的以下方法:

import itertools, operator

l = [{'queryID': '1', 'documentID': '184'},
{'queryID': '1', 'documentID': '29'},
{'queryID': '1', 'documentID': '31'},
 {'queryID': '1', 'documentID': '12'},
{'queryID': '2', 'documentID': '12'},
{'queryID': '2', 'documentID': '15'},
{'queryID': '2', 'documentID': '184'},
{'queryID': '2', 'documentID': '858'},
{'queryID': '2', 'documentID': '51'},
 {'queryID': '2', 'documentID': '102'}]

l.sort(key=operator.itemgetter('queryID'))

result = [{'queryID': k, 'documentID': [d['documentID'] for d in v ]}
          for k,v in itertools.groupby(l, key=operator.itemgetter('queryID'))]

print(result)

输出:

[{'documentID': ['184', '29', '31', '12'], 'queryID': '1'}, {'documentID': ['12', '15', '184', '858', '51', '102'], 'queryID': '2'}]