我有一个字典列表,如下所示:
[{'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}]
答案 0 :(得分:2)
您希望按queryID
键对字典列表进行分组,并获取每个组的documentID
列表。
在这种情况下,预期输出应包含documentID
s的序列使用itertools.groupby
和operator.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'}]