Python - 列表中去除重複字典


Python 是一種非常廣泛使用的平臺,用於 Web 開發、資料科學、機器學習以及執行不同的自動化流程。我們可以使用不同的資料型別(如列表、字典、資料集)在 Python 中儲存資料。Python 字典中的資料和資訊可以根據我們的選擇進行編輯和更改。

以下文章將提供有關從列表中刪除重複字典的不同方法的資訊。沒有直接選擇重複字典的選項,因此我們必須使用 Python 的不同方法和功能來刪除這些字典。

去除重複字典的各種方法

列表推導式

由於我們不能直接比較列表中的不同字典,我們必須將它們轉換為其他形式,以便我們可以比較存在的不同字典。我們可以透過以下示例更好地理解這一點。

示例

def all_duplicate(whole_dict):  
    same = set()   #We check all the dictionaries with the help of same set created
    return [dict(tuple(sorted(dupl.items()))) for dupl in whole_dict if tuple(sorted(dupl.items())) not in same and not same.add(tuple(sorted(dupl.items())))]  #We will convert each dictionary into tuple so that the dictionary having the same value will be removed and the duplicate dictionary can be found easily, if the tuple has a different value then the dictionary will be kept. 

# Example 
Whole_Dictionary = [
    {"Place": "Haldwani", "State": 'Uttrakhand'},
    {"Place": "Hisar", "State": 'Haryana'},
    {"Place": "Shillong", "State": 'Meghalaya'},
    {"Place": "Kochi", "State": 'Kerala'},
    {"Place": "Bhopal", "State": 'Madhya Pradesh'},
    {"Place": "Kochi", "State": 'Kerala'},   #This Dictionary is repeating which is to be removed
    {"Place": "Haridwar", "State": 'Uttarakhand'}
]

Final_Dict = all_duplicate(Whole_Dictionary)
print(Final_Dict)   #The output after removing the duplicate dictionary will be shown

輸出

上述示例的輸出如下所示。

[{'Place': 'Haldwani', 'State': 'Uttrakhand'}, {'Place': 'Hisar', 'State': 'Haryana'}, {'Place': 'Shillong', 'State': 'Meghalaya'}, {'Place': 'Kochi', 'State': 'Kerala'}, {'Place': 'Bhopal', 'State': 'Madhya Pradesh'}, {'Place': 'Haridwar', 'State': 'Uttarakhand'}] 

Pandas 庫

此方法僅用於包含許多不同元素的大量資料集的情況,即僅用於包含複雜資料的字典。我們可以透過以下示例瞭解 Pandas 庫的使用。

示例

import pandas as ps   #Do not forget to import pandas or error might occur
#Convert the dictionaries into panda frame

def all_duplicate(data):
    dd = ps.DataFrame(data)
    dd.drop_duplicates(inplace=True)   #Drop_duplicates() method will remove all the duplicate dictionaries
    return dd.to_dict(orient='records')  #Converting dictionaries back into list of dictionaries from panda frame

# Example 
Whole_Dictionary = [
    {"Place": "Haldwani", "State": 'Uttrakhand'},
    {"Place": "Hisar", "State": 'Haryana'},
    {"Place": "Shillong", "State": 'Meghalaya'},
    {"Place": "Kochi", "State": 'Kerala'},
    {"Place": "Bhopal", "State": 'Madhya Pradesh'},
    {"Place": "Kochi", "State": 'Kerala'},   #This Dictionary is repeating which is to be removed
    {"Place": "Haridwar", "State": 'Uttarakhand'}
]

Final_Dict = all_duplicate(Whole_Dictionary)
print(Final_Dict)   #The output after removing the duplicate dictionary will be shown

輸出

[{'Place': 'Haldwani', 'State': 'Uttrakhand'}, {'Place': 'Hisar', 'State': 'Haryana'}, {'Place': 'Shillong', 'State': 'Meghalaya'}, {'Place': 'Kochi', 'State': 'Kerala'}, {'Place': 'Bhopal', 'State': 'Madhya Pradesh'}, {'Place': 'Haridwar', 'State': 'Uttarakhand'}] 

凍結字典

使用凍結字典的概念是一種解決字典不可雜湊性的技術。凍結字典可以作為另一個字典中的鍵或集合中的元素使用,因為它本質上是字典的不可變形式。frozendict 庫提供了一種方便的凍結字典實現。我們可以透過以下示例更好地理解這一點。

示例

def make_hashable(d):
    return hash(frozenset(d.items())) # We will convert the dictionary key values into frozen set and then pass it to hash function

def all_duplicate(dicts):
    seen = set()  #It will check for similarities in the list
    return [d for d in dicts if not (make_hashable(d) in seen or seen.add(make_hashable(d)))] #If similarity will be found it will be removed and if not then the data will be kept

# Example 
Whole_Dictionary = [
    {"Place": "Haldwani", "State": 'Uttrakhand'},
    {"Place": "Hisar", "State": 'Haryana'},
    {"Place": "Shillong", "State": 'Meghalaya'},
    {"Place": "Kochi", "State": 'Kerala'},
    {"Place": "Bhopal", "State": 'Madhya Pradesh'},
    {"Place": "Kochi", "State": 'Kerala'},   #This Dictionary is repeating which is to be removed
    {"Place": "Haridwar", "State": 'Uttarakhand'}
]

Final_Dict = all_duplicate(Whole_Dictionary)
print(Final_Dict)   #The output after removing the duplicate dictionary will be shown

輸出

[{'Place': 'Haldwani', 'State': 'Uttrakhand'}, {'Place': 'Hisar', 'State': 'Haryana'}, {'Place': 'Shillong', 'State': 'Meghalaya'}, {'Place': 'Kochi', 'State': 'Kerala'}, {'Place': 'Bhopal', 'State': 'Madhya Pradesh'}, {'Place': 'Haridwar', 'State': 'Uttarakhand'}  

輔助函式

這是從字典列表中刪除重複字典的一種複雜方法。在此過程中,透過使用輔助函式,每個字典都被轉換為其內容的排序元組。然後使用此輔助函式查詢重複的元組並將其從字典列表中刪除。我們可以透過以下示例更好地理解。

示例

def sorted_dict_to_tuple(d):  # sorted_dicts_to_tuple takes the dictionary as input and sorts it into tuple
    return tuple(sorted(d.items()))

def all_duplicates(dicts):  # The all_duplicates function will check all the elements in the dictionary and keep track of any repeating element
    seen = set() 
    return [d for d in dicts if not (sorted_dict_to_tuple(d) in seen or seen.add(sorted_dict_to_tuple(d)))]

# Example 
Whole_Dictionary = [
    {"Place": "Haldwani", "State": 'Uttrakhand'},
    {"Place": "Hisar", "State": 'Haryana'},
    {"Place": "Shillong", "State": 'Meghalaya'},
    {"Place": "Kochi", "State": 'Kerala'},
    {"Place": "Bhopal", "State": 'Madhya Pradesh'},
    {"Place": "Kochi", "State": 'Kerala'},   #This Dictionary is repeating which is to be removed
    {"Place": "Haridwar", "State": 'Uttarakhand'}
]

Final_Dict = all_duplicates(Whole_Dictionary)
print(Final_Dict)   #The output after removing the duplicate dictionary will be shown

輸出

[{'Place': 'Haldwani', 'State': 'Uttrakhand'}, {'Place': 'Hisar', 'State': 'Haryana'}, {'Place': 'Shillong', 'State': 'Meghalaya'}, {'Place': 'Kochi', 'State': 'Kerala'}, {'Place': 'Bhopal', 'State': 'Madhya Pradesh'}, {'Place': 'Haridwar', 'State': 'Uttarakhand'}]  

結論

遵循正確的步驟至關重要,因為從列表中刪除重複字典是一項耗時且複雜的任務。本文列出了可以用來從列表中刪除重複字典的每種方法。人們可以根據自己的方便和應用領域使用任何方法。

更新於:2023年8月1日

774 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.