[英]Remove duplicates dictionaries from list of dictionaries in python
I want to remove duplicates dictionaries from list of dictionaries.我想从字典列表中删除重复的字典。 I am trying to make configurable code to work on any field instead of making field specific.我正在尝试使可配置代码适用于任何领域,而不是特定领域。
Input Data输入数据
dct = {'Customer_Number': 90617174,
'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [12177218280.0]},
{'Phone_Type': 'Mobile', 'Phone': [12177218280.0]}],
'Email': [{'Email_Type': 'Primary',
'Email': ['saman.zonouz@rutgers.edu']},
{'Email_Type': 'Primary',
'Email': ['saman.zonouz@rutgers.edu']}]
}
Output Data: Output 数据:
deduped_dict = {'Customer_Number': 90617174,
'Email': [{'Email_Type': 'Primary',
'Email': ['saman.zonouz@rutgers.edu']}],
'Phone_Number': [{'Phone_Type': 'Mobile',
'Phone': [12177218280]}]}
**Code tried:**
res_list = []
for i in range(len(dic)):
if dic[i] not in dic[i + 1:]:
res_list.append(dic[i])
Write a function to dedupe lists:将 function 写入重复数据删除列表:
def dedupe_list(lst):
result = []
for el in lst:
if el not in result:
result.append(el)
return result
def dedupe_dict_values(dct):
result = {}
for key in dct:
if type(dct[key]) is list:
result[key] = dedupe_list(dct[key])
else:
result[key] = dct[key]
return result
# Test it:
dedupe_dict_values(dct) == deduped_dict
## Out[12]: True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.