我有一个列表:
list=[{'Query': 'documents'}, {'entity': 'a'}, {'value': 'b'}, {'entity': 'c'}, {'value': 'd'}, {'records':21}]
我要关注o / p:
d={'Query': ['documents'],'entity': ['a','c'],'value': ['b','d'],'records':[21]}
答案 0 :(得分:0)
尝试此代码:
list=[{'Query': 'documents'}, {'entity': 'a'}, {'value': 'b'}, {'entity': 'c'}, {'value': 'd'}, {'records':21}]
d={}
for i in list :
for j,k in i.items():
l = []
if j in d:
l = d[j]
l.append(k)
d[j] = l
else:
l.append(str(k))
d[j]=l
print(d)
输出:
{'records': ['21'], 'value': ['b', 'd'], 'entity': ['a', 'c'], 'Query': ['documents']}