我有一个像这样的字典的python列表:
[{'George': {u'finance': {u'investing': {u'venture capital': 'Peter'}}}},
{'George': {u'finance': {u'investing': {u'venture capital': 'John'}}}},
{'George': {u'finance': {u'investing': {u'venture capital': 'Kate'}}}}]
我想分组,所以最后的结果将是:
{'George': {u'finance': {u'investing': {u'venture capital': ['Peter','John','Kate'}}}}
如何将此词典列表分组?
答案 0 :(得分:1)
到目前为止你尝试了什么?
如果你的数据结构如上例所示得到修复,你可以尝试这样的事情:
merged_dict = {}
for d in your_list_of_dicts:
for k in d:
if k not in merged_dict.keys():
merged_dict[k] = d[k]
merged_dict[k]['finance']['investing']['venture capital'] = [merged_dict[k]['finance']['investing']['venture capital']]
else:
merged_dict[k]['finance']['investing']['venture capital'].append(d[k]['finance']['investing']['venture capital'])