假设我有一个字典列表:
list = [{'name':'john','age':'28','location':'hawaii','gender':'male'},
{'name':'john','age':'32','location':'colorado','gender':'male'},
{'name':'john','age':'32','location':'colorado','gender':'male'},
{'name':'parker','age':'24','location':'new york','gender':'male'}]
在这个字典中,'name' 可以被认为是一个唯一的标识符。我的目标不仅是为相同的字典(即列表 [1] 和列表 [2])删除此列表,而且还为单个“名称”(即列表 [0] 和列表 [1/2 ]. 换句话说,我想将示例中的所有 'name'='john' 字典合并到一个字典中,如下所示:
dedup_list = [{'name':'john','age':'28; 32','location':'hawaii; colorado','gender':'male'},
{'name':'parker','age':'24','location':'new york','gender':'male'} ]
到目前为止,我已经尝试创建我的第二个列表 dedup_list,并遍历第一个列表。如果 dedup_list 的字典之一中不存在“name”键,我将附加它。这是我卡住的合并部分。
for dict in list:
for new_dict in dedup_list:
if dict['name'] in new_dict:
# MERGE OTHER DICT FIELDS HERE
else:
dedup_list.append(dict) # This will create duplicate values as it iterates through each row of the dedup_list. I can throw them in a set later to remove?
我的 dicts 列表永远不会包含超过 100 个项目,因此 O(n^2) 解决方案绝对是可以接受的,但不一定是理想的。这个 dedup_list 最终将被写入 CSV,所以如果有解决方案,我会全力以赴。
谢谢!