简体   繁体   English

如果值重复,如何从字典中删除重复项

[英]How to remove duplicates from the dictionary if values repeats

I have the following list of dict:我有以下字典列表:

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' } ] 

I want to remove the dictionary if the email repeats如果电子邮件重复,我想删除字典

Code is below代码如下

[dict(element) for element in {tuple(each.items()) for each in test }]

My code is working only all the keys and values are same.我的代码仅在所有键和值都相同时才起作用。 My requirement is delete the duplicates dictionary for the email我的要求是删除电子邮件的重复字典

seen = set()
result = [d for d in test if d['Email'] not in seen and not seen.add(d['Email'])]

You could also use pandas in the following way:您还可以通过以下方式使用熊猫:

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')

From your question, it isn't clear how you want to choose the entries to keep (first instance or something else) but you could apply some more sophisticated method of dropping using pandas.从您的问题来看,尚不清楚您想如何选择要保留的条目(第一个实例或其他内容),但您可以应用一些更复杂的使用 Pandas 删除的方法。

You need to create a new list with only the unique items as below:您需要创建一个仅包含以下唯一项目的新列表:

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()

You can use something like that你可以使用类似的东西

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)

Also

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 will now have the latest list of unique dictionaries. test现在将拥有最新的独特词典列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM