字典是Python中常用的功能,用于根据用户的需要存储数据。另一个典型的过程涉及编辑或操作这些数据。要成为一个高效和快速的程序员,你必须弄清楚如何从字典列表中删除一个字典。本文将介绍从字典列表中删除字典的许多技巧。
从字典列表中删除字典的不同方法
循环方法
我们将指定要从字典列表中删除的字典,然后我们将使用if()创建一个条件来提供一个参数,以从字典列表中删除该字典。我们可以通过以下示例更清楚地理解:
Example
的中文翻译为:示例
# Dictionaries Cities = [ {"City": "Bangalore", "location": "India"}, {"City": "Toronto", "location": "Canada"}, {"City": "Liverpool", "location": "England"}, {"City": "kano", "location": "Nigeria"}, {"City": "Sydney", "location": "Australia"}, {"City": "Berlin", "location": "Germany"}, {"City": "New York", "location": "USA"} ] Remove = "Liverpool" #Specifying the dictionary to be removed for city in Cities: # Checking all the different dictionaries if city["City"] == Remove: #Creating a condition Cities.remove(city) #If the condition is satisfied remove() method will be used print(Cities) #Display the output after removing the dictionary
输出
程序的输出将如下所示:
[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]
列表推导式
通过使用列表推导方法,我们可以通过应用条件来删除特定的字典,然后我们可以创建一个修改后的字典列表,其中不包含指定的字典。我们可以通过以下示例更清楚地理解:
Example
的中文翻译为:示例
#Dictionaries Cities = [ {"City": "Bangalore", "location": "India"}, {"City": "Toronto", "location": "Canada"}, {"City": "Liverpool", "location": "England"}, {"City": "kano", "location": "Nigeria"}, {"City": "Sydney", "location": "Australia"}, {"City": "Berlin", "location": "Germany"}, {"City": "New York", "location": "USA"} ] Remove = "Liverpool" #Specifying Dictionary To Be Removed Cities = [city for city in Cities if city["City"] != Remove] #Creating a new list and specifying the condition to remove the unwanted dictionary print(Cities) #Display The Updated Output
输出
上述程序的输出将如下所示:
[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]
改变原始列表
在这种方法中,我们不会创建任何新的列表,而是直接在原始字典列表中进行更改。因此,这样做既简单又快速,也不会产生数据重复。我们可以通过以下示例更清楚地理解:
Example
的中文翻译为:示例
# Dictionaries Cities = [ {"City": "Bangalore", "location": "India"}, {"City": "Toronto", "location": "Canada"}, {"City": "Liverpool", "location": "England"}, {"City": "kano", "location": "Nigeria"}, {"City": "Sydney", "location": "Australia"}, {"City": "Berlin", "location": "Germany"}, {"City": "New York", "location": "USA"} ] for City in Cities: #We will specify a condition if City.get("location") == 'England': #If the location is England Cities.remove(City) #Remove the dictionary with location as England print(Cities) #Display The Modified Output
输出
上述代码的输出结果如下:
[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]
过滤函数
正如其名称所示,我们将简单地应用一个过滤器来指定要从字典列表中删除的字典。我们可以通过以下示例更好地理解:
Example
的中文翻译为:示例
#Dictionaries Cities = [ {"City": "Bangalore", "location": "India"}, {"City": "Toronto", "location": "Canada"}, {"City": "Liverpool", "location": "England"}, {"City": "kano", "location": "Nigeria"}, {"City": "Sydney", "location": "Australia"}, {"City": "Berlin", "location": "Germany"}, {"City": "New York", "location": "USA"} ] new_dictionary = list(filter(lambda City: City.get("location") != 'England', Cities)) # We specified a condition that if the location is England is found from the list then it is to be filtered out and removed from the list of dictionaries print(new_dictionary) #Display the Modified Output
输出
上述程序的输出将如下所示:
[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]
列表索引
这种方法仅在字典列表较小且您知道要删除的字典的确切位置时使用。因此,您只需指定要删除的字典的位置。让我们举一个例子来更清楚地理解:
Example
的中文翻译为:示例
#Dictionaries Cities = [ {"City": "Bangalore", "location": "India"}, {"City": "Toronto", "location": "Canada"}, {"City": "Liverpool", "location": "England"}, {"City": "kano", "location": "Nigeria"}, {"City": "Sydney", "location": "Australia"}, {"City": "Berlin", "location": "Germany"}, {"City": "New York", "location": "USA"} ] dictionary_remove= 2 #It specifies the position of the dictionary to be removed #The index number starts from 0 del Cities[dictionary_remove] #It commands to delete the dictionary in specified index number print(Cities) #Displays the Modified Output
输出
上述程序的输出将如下所示:
[{'City': 'Bangalore', 'location': 'India'}, {'City': 'Toronto', 'location': 'Canada'}, {'City': 'kano', 'location': 'Nigeria'}, {'City': 'Sydney', 'location': 'Australia'}, {'City': 'Berlin', 'location': 'Germany'}, {'City': 'New York', 'location': 'USA'}]
结论
在处理大量数据时,修改数据是一个必要的步骤。因此,了解各种技术以便快速实施修改是非常重要的。
本文详细介绍了从包含在数据源中的字典列表中删除字典的所有可能方式。在进行这种类型的操作时,您必须保持警惕,因为可能会出现数据错误,可能导致数据丢失。因此,在对数据进行任何更改之前,有必要先备份数据。
以上是Python - 从字典列表中删除字典的详细内容。更多信息请关注PHP中文网其他相关文章!

Python和C 各有优势,选择应基于项目需求。1)Python适合快速开发和数据处理,因其简洁语法和动态类型。2)C 适用于高性能和系统编程,因其静态类型和手动内存管理。

选择Python还是C 取决于项目需求:1)如果需要快速开发、数据处理和原型设计,选择Python;2)如果需要高性能、低延迟和接近硬件的控制,选择C 。

通过每天投入2小时的Python学习,可以有效提升编程技能。1.学习新知识:阅读文档或观看教程。2.实践:编写代码和完成练习。3.复习:巩固所学内容。4.项目实践:应用所学于实际项目中。这样的结构化学习计划能帮助你系统掌握Python并实现职业目标。

在两小时内高效学习Python的方法包括:1.回顾基础知识,确保熟悉Python的安装和基本语法;2.理解Python的核心概念,如变量、列表、函数等;3.通过使用示例掌握基本和高级用法;4.学习常见错误与调试技巧;5.应用性能优化与最佳实践,如使用列表推导式和遵循PEP8风格指南。

Python适合初学者和数据科学,C 适用于系统编程和游戏开发。1.Python简洁易用,适用于数据科学和Web开发。2.C 提供高性能和控制力,适用于游戏开发和系统编程。选择应基于项目需求和个人兴趣。

Python更适合数据科学和快速开发,C 更适合高性能和系统编程。1.Python语法简洁,易于学习,适用于数据处理和科学计算。2.C 语法复杂,但性能优越,常用于游戏开发和系统编程。

每天投入两小时学习Python是可行的。1.学习新知识:用一小时学习新概念,如列表和字典。2.实践和练习:用一小时进行编程练习,如编写小程序。通过合理规划和坚持不懈,你可以在短时间内掌握Python的核心概念。

Python更易学且易用,C 则更强大但复杂。1.Python语法简洁,适合初学者,动态类型和自动内存管理使其易用,但可能导致运行时错误。2.C 提供低级控制和高级特性,适合高性能应用,但学习门槛高,需手动管理内存和类型安全。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)