[英]Remove duplicates values from list of dictionaries in python
I want to remove duplicates values from list which is inside the dictionary.我想从字典内的列表中删除重复值。 I am trying to make configurable code to work on any field instead of making field specific.
我正在尝试使可配置代码适用于任何领域,而不是特定领域。
Input Data:输入数据:
{'Customer_Number': 90617174, 'Email': [{'Email_Type': 'Primary', 'Email': ['saman.zonouz@rutgers.edu', 'saman.zonouz@rutgers.edu']}], 'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [12177218280, 12177218280]}]}
Expected Output Data:预期 Output 数据:
{'Customer_Number': 90617174, 'Email': [{'Email_Type': 'Primary', 'Email': ['saman.zonouz@rutgers.edu']}], 'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [12177218280]}]}
code tried:代码尝试:
dic = {'Customer_Number': 90617174, 'Email': [{'Email_Type': 'Primary', 'Email': ['saman.zonouz@rutgers.edu', 'saman.zonouz@rutgers.edu']}], 'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [12177218280, 12177218280]}]}
res = []
for i in dic:
if i not in res:
res.append(i)
You can use set()
您可以使用
set()
import json
dic = {
'Customer_Number': 90617174,
'Email': [
{
'Email_Type': 'Primary',
'Email': list(set([
'saman.zonouz@rutgers.edu',
'saman.zonouz@rutgers.edu',
]))
}
],
'Phone_Number': [
{
'Phone_Type': 'Mobile',
'Phone': list(set([
12177218280,
12177218280,
]))
}
]
}
print(json.dumps(dic,indent=2))
If you want to do it on a list of dic
's then you can do like this:如果您想在
dic
列表上执行此操作,则可以这样做:
for dic in dics:
for email in dic['Email']:
email['Email'] = list(set(email['Email']))
for phone in dic['Phone_Number']:
phone['Phone'] = list(set(phone['Phone']))
The approach that you started with, you need to go a few levels deeper with that to find every such "repeating" list and dedupe it.您开始使用的方法,您需要将 go 更深一些级别,以找到每个这样的“重复”列表并对其进行重复数据删除。
To dedupe, you can use a set
- which is also a "container" data structure like a list
but with some (many?) differences.要进行重复数据删除,您可以使用一个
set
——它也是一个像list
一样的“容器”数据结构,但有一些(很多?)差异。 You can get a good introduction to all of this in the official python docs -您可以在官方 python 文档中对所有这些进行很好的介绍 -
for key in dic:
if isinstance(dic[key], list):
for inner_dict in dic[key]:
for inner_key in inner_dict:
if isinstance(inner_dict[inner_key], list):
inner_dict[inner_key] = list(set(inner_dict[inner_key]))
print(dic)
#{'Customer_Number': 90617174,
# 'Email': [{'Email_Type': 'Primary', 'Email': ['saman.zonouz@rutgers.edu']}],
# 'Phone_Number': [{'Phone_Type': 'Mobile', 'Phone': [12177218280]}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.