前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python - 删除列表中的重复字典

Python - 删除列表中的重复字典

作者头像
很酷的站长
发布2023-08-11 15:29:18
2721
发布2023-08-11 15:29:18
举报
文章被收录于专栏:站长的编程笔记
Python - 删除列表中的重复字典
Python - 删除列表中的重复字典

Python 是一个非常广泛使用的平台,用于 Web 开发、数据科学、机器学习以及自动化执行不同的过程。我们可以将数据存储在python中,以不同的数据类型,例如列表,字典,数据集。python字典中的数据和信息可以根据我们的选择进行编辑和更改

下面的文章将提供有关删除列表中重复词典的不同方法的信息。直接选择重复词典的选项不可用,因此我们将不得不使用 python 的不同方法和功能来删除词典。

删除重复词典的各种方法

列表理解

由于我们无法直接比较列表中的不同词典,因此我们将不得不将它们转换为其他形式,以便我们可以比较存在的不同词典。通过以下示例,我们可以更好地理解它:

代码语言:javascript
复制
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

输出

上面示例的输出如下所示:

代码语言:javascript
复制
[{'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'}]

熊猫图书馆

此方法仅在具有许多不同元素的大量数据集的情况下使用,也就是说,仅适用于具有复杂数据的字典。我们可以通过下面的例子来理解熊猫库的使用:

代码语言:javascript
复制
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

输出

代码语言:javascript
复制
[{'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'}]

冰雪奇缘词典

使用冻结词典的想法是解决词典不可散列性的一种技术。冻结字典可以用作另一个字典中的键或集合中的元素,因为它本质上是字典的不可变形式。冻结词典库提供了冻结词典的便捷实现。通过以下示例,我们可以更好地理解它:

代码语言:javascript
复制
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

输出

代码语言:javascript
复制
[{'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'}

辅助函数

这是一种从词典列表中删除重复词典的复杂方法。通过使用帮助程序函数,在此过程中,每个字典都转换为其内容的排序元组。然后使用此辅助功能从字典列表中找到重复的元组并将其删除。通过以下示例,我们可以更好地理解它:

代码语言:javascript
复制
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

输出

代码语言:javascript
复制
[{'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'}]

结论

遵循正确的过程至关重要,因为从列表中删除重复词典是一项耗时且困难的任务。本文列出了可用于从列表中消除重复词典的所有方法。可以根据其便利性和应用领域使用任何方法。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 删除重复词典的各种方法
    • 列表理解
        • 输出
          • 熊猫图书馆
              • 输出
                • 冰雪奇缘词典
                    • 输出
                      • 辅助函数
                          • 输出
                          • 结论
                          相关产品与服务
                          数据保险箱
                          数据保险箱(Cloud Data Coffer Service,CDCS)为您提供更高安全系数的企业核心数据存储服务。您可以通过自定义过期天数的方法删除数据,避免误删带来的损害,还可以将数据跨地域存储,防止一些不可抗因素导致的数据丢失。数据保险箱支持通过控制台、API 等多样化方式快速简单接入,实现海量数据的存储管理。您可以使用数据保险箱对文件数据进行上传、下载,最终实现数据的安全存储和提取。
                          领券
                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档