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

Python - 从字典列表中删除字典

作者头像
很酷的站长
发布2023-08-11 15:29:43
1370
发布2023-08-11 15:29:43
举报
Python - 从字典列表中删除字典
Python - 从字典列表中删除字典

字典是python的一个非常常用的功能,用于根据用户需要在其中存储数据。另一个典型的过程涉及编辑或操作此数据。要成为一名高效且快速的程序员,您必须弄清楚如何从字典列表中删除字典。有许多技术可以从词典列表中删除字典,本文将介绍这些技术。

从字典列表中删除字典的不同方法

循环方式

我们将指定要从字典列表中删除的字典,然后我们将使用 if() 创建一个条件来提供一个参数以从字典列表中删除字典。通过以下示例,我们可以更清楚地理解:

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

输出

程序的输出将如下所示:

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

列表理解

通过使用列表推导方法,我们将通过应用条件删除特定的字典,然后我们可以创建一个修改后的字典列表的新列表,而无需指定的字典。通过下面的例子,我们可以更清楚地理解它:

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

输出

上述程序的输出如下:

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

更改原始列表

在这种方法中,我们不会创建任何新列表,而是直接在原始字典列表中进行更改。因此,这使得工作变得简单快捷,并且不会重复数据。通过下面的例子,我们可以更清楚地理解它:

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

输出

上述代码的输出如下:

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

过滤功能

顾名思义,我们将简单地应用一个过滤器来指定要从字典列表中删除的字典。通过以下示例,我们可以更好地理解它:

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

输出

上述程序的输出如下:

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

列表索引

此方法仅在知道要删除的字典的确切位置时,仅在字典列表较小的情况下使用。因此,您只需指定字典的位置即可将其删除。让我们举一个例子来更清楚地理解它:

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

输出

上述程序的输出如下:

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

结论

处理大量数据时,更改数据是该过程的必要步骤。因此,必须了解各种技术才能快速实施修改。

本文详细介绍了从数据源中包含的词典列表中删除词典的所有可能方法。使用此类方法时,您必须注意,因为可能会出现可能导致数据丢失的数据错误。因此,在对数据进行任何更改之前,必须备份数据。

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 从字典列表中删除字典的不同方法
    • 循环方式
        • 输出
          • 列表理解
              • 输出
                • 更改原始列表
                    • 输出
                      • 过滤功能
                          • 输出
                            • 列表索引
                                • 输出
                                • 结论
                                领券
                                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档