[英]How to remove the duplicates values from only one element of the dictionary at a time?
[英]How to remove duplicates from the dictionary if values repeats
我有以下字典列表:
test = [ { 'id': '195', 'Name': 'i', 'Email': 'chdtn@gmail.com', 'Account': 'Product' }, { 'id': '219', 'Name': 'umar', 'Email': 'ddhi@gmail.com', 'Account': 'Product' }, { 'id': '74', 'Name': 'Are', 'Email': 'ddhit@gmail.com', 'role': 'Tester' }, { 'id': '24', 'Name': 'Mee', 'Email': 'huul@gmail.com', 'role': 'Tester' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' }, { 'id': '220', 'Name': 'Sdy', 'Email': 'deyan@gmail.com', 'role': 'Product' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' } ]
如果电子邮件重复,我想删除字典
代码如下
[dict(element) for element in {tuple(each.items()) for each in test }]
我的代码仅在所有键和值都相同时才起作用。 我的要求是删除电子邮件的重复字典
seen = set()
result = [d for d in test if d['Email'] not in seen and not seen.add(d['Email'])]
您还可以通过以下方式使用熊猫:
import pandas as pd
test = [ { 'id': '195', 'Name': 'i', 'Email': 'chdtn@gmail.com', 'Account': 'Product' }, { 'id': '219', 'Name': 'umar', 'Email': 'ddhi@gmail.com', 'Account': 'Product' }, { 'id': '74', 'Name': 'Are', 'Email': 'ddhit@gmail.com', 'role': 'Tester' }, { 'id': '24', 'Name': 'Mee', 'Email': 'huul@gmail.com', 'role': 'Tester' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' }, { 'id': '220', 'Name': 'Sdy', 'Email': 'deyan@gmail.com', 'role': 'Product' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' } ]
df = pd.DataFrame(test)
df_res = df.drop_duplicates('Email')
从您的问题来看,尚不清楚您想如何选择要保留的条目(第一个实例或其他内容),但您可以应用一些更复杂的使用 Pandas 删除的方法。
您需要创建一个仅包含以下唯一项目的新列表:
unique_emails=[]
test_with_unique_emails = []
for item in test:
if item["Email"] not in unique_emails:
test_with_unique_emails.append(item)
unique_emails.append(item["Email"])
dict([(d['Email'], d) for d in test]).values()
你可以使用类似的东西
test = [ { 'id': '195', 'Name': 'i', 'Email': 'chdtn@gmail.com', 'Account': 'Product' }, { 'id': '219', 'Name': 'umar', 'Email': 'ddhi@gmail.com', 'Account': 'Product' }, { 'id': '74', 'Name': 'Are', 'Email': 'ddhit@gmail.com', 'role': 'Tester' }, { 'id': '24', 'Name': 'Mee', 'Email': 'huul@gmail.com', 'role': 'Tester' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' }, { 'id': '220', 'Name': 'Sdy', 'Email': 'deyan@gmail.com', 'role': 'Product' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' } ] <br>
unique_emails = []
final = []
for element in test:
if element['Email'] not in unique_emails:
final.append(element)
print(final)
还
import ast
test = [ { 'id': '195', 'Name': 'i', 'Email': 'chdtn@gmail.com', 'Account': 'Product' }, { 'id': '219', 'Name': 'umar', 'Email': 'ddhi@gmail.com', 'Account': 'Product' }, { 'id': '74', 'Name': 'Are', 'Email': 'ddhit@gmail.com', 'role': 'Tester' }, { 'id': '24', 'Name': 'Mee', 'Email': 'huul@gmail.com', 'role': 'Tester' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' }, { 'id': '220', 'Name': 'Sdy', 'Email': 'deyan@gmail.com', 'role': 'Product' }, { 'id': '230', 'Name': 'San', 'Email': 'deyan@gmail.com', 'role': 'Tester' } ]
print([ast.literal_eval(el1) for el1 in set([str(el2) for el2 in test])])
emails = []
for t in test:
if t['Email'] in emails:
emails.append(t['Email'])
else:
test.remove(t)
test
现在将拥有最新的独特词典列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.