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'}]
熊猫图书馆
此方法仅在具有许多不同元素的大量数据集的情况下使用,也就是说,仅适用于具有复杂数据的字典。我们可以通过下面的例子来理解熊猫库的使用:
例
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'}]
冰雪奇缘词典
使用冻结词典的想法是解决词典不可散列性的一种技术。冻结字典可以用作另一个字典中的键或集合中的元素,因为它本质上是字典的不可变形式。冻结词典库提供了冻结词典的便捷实现。通过以下示例,我们可以更好地理解它:
例
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'}]
结论
遵循正确的过程至关重要,因为从列表中删除重复词典是一项耗时且困难的任务。本文列出了可用于从列表中消除重复词典的所有方法。可以根据其便利性和应用领域使用任何方法。
本站发布的内容若侵犯到您的权益,请邮件联系站长删除,我们将及时处理!
从您进入本站开始,已表示您已同意接受本站【免责声明】中的一切条款!
本站大部分下载资源收集于网络,不保证其完整性以及安全性,请下载后自行研究。
本站资源仅供学习和交流使用,版权归原作者所有,请勿商业运营、违法使用和传播!请在下载后24小时之内自觉删除。
若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。