当前位置:首页 > 编程技术 > 正文内容

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

yc8889个月前 (08-02)编程技术213

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小时之内自觉删除。


若作商业用途,请购买正版,由于未及时购买和付费发生的侵权行为,使用者自行承担,概与本站无关。


本文链接:https://www.10zhan.com/biancheng/10663.html

标签: Python
分享给朋友:

“Python - 删除列表中的重复字典” 的相关文章

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

【说站】C#在PDF中添加墨迹注释Ink Annotation的步骤详解

PDF中的墨迹注释(Ink Annotation),表现为徒手涂鸦式的形状;该类型的注释,可任意指定形状顶点的位置及个数,通过指定的顶点,程序将连接各点绘制成平滑的曲线。下面,通过C#程序代码介绍如何...

【说站】Java从resources读取文件内容的方法有哪些

【说站】Java从resources读取文件内容的方法有哪些

本文主要介绍的是java读取resource目录下文件的方法,比如这是你的src目录的结构├── main│ ├── java│ │ └── ...

【说站】PHP使用Openssl实现本地生成csr、key、crt证书文件

【说站】PHP使用Openssl实现本地生成csr、key、crt证书文件

在Apache中要启用HTTPS访问,需要开启Openssl,也就需要crt和key两个和证书相关的文件了,那如果用制作呢?之前发过博文介绍过用在线生成的方式,但搞PHP编程的人有些东西还是想在自己的...

【说站】jenkins配置ssh服务器并配置ssh servers

【说站】jenkins配置ssh服务器并配置ssh servers

1.系统管理-》配置-》Publish over SSH当然,没有这个插件的第一时间去插件管理里面去下载2.生成秘钥任意找一台服务器,生成即可。2.1已有秘钥无须在生成,只需要吧公钥配置到将要配置的s...

【说站】前端使用Image()函数加载base64图片不兼容无onload事件解决方法

【说站】前端使用Image()函数加载base64图片不兼容无onload事件解决方法

Image()函数将会创建一个新的HTMLImageElement实例。它的功能等价于 document.createElement('img')。正常情况下,我们使用下面方法加载图片...

【说站】嵌入式Linux下完成LCD屏文字显示(帧缓冲框架)

【说站】嵌入式Linux下完成LCD屏文字显示(帧缓冲框架)

帧缓冲框架是Linux下专门为显示类设备设计的接口,目的是将硬件和软件层分离开,方便应用层的编程,也方便应用层程序移植。帧缓冲框架向驱动层和应用层分别提供了一套标准接口,驱动层按照框架编写驱动,应用层...