Ark*_*tos 3 python dictionary structure list set
我想在列表中加入词典,其关键"用户"是相同的,但我没有意识到如何.例如:
[{'count2': 34, 'user': 2},
{'count4': 233, 'user': 2},
{'count2': 234, 'user': 4},
{'count4': 344, 'user': 5}]
Run Code Online (Sandbox Code Playgroud)
会成为:
[{'count2': 34, 'count4': 233, 'user': 2 },
{'count2': 234, 'user': 4},
{'count4': 344, 'user': 5}]
Run Code Online (Sandbox Code Playgroud)
我广泛搜索没有发现堆栈溢出类似的东西,任何帮助将不胜感激.
from collections import defaultdict
dl = [{'count2': 34, 'user': 2},
{'count4': 233, 'user': 2},
{'count2': 234, 'user': 4},
{'count4': 344, 'user': 5}]
print dl
dd = defaultdict(dict)
for d in dl:
dd[d['user']].update(d)
print dd.values()
Run Code Online (Sandbox Code Playgroud)