簡體   English   中英

如果值重復,如何從字典中刪除重復項

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM