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


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

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

循环方法

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

示例

# 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'}]  

列表推导式

通过使用列表推导式方法,我们将通过应用条件删除特定的字典,然后我们可以创建一个新的修改后的字典列表,其中不包含指定的字典。我们可以通过以下示例更清楚地理解它

示例

#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'}]  

更改原始列表

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

示例

# 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'}] 

filter 函数

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

示例

#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'}]   

列表索引

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

示例

#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'}]  

结论

在处理大量数据时,更改数据是流程中的必要步骤。因此,必须了解各种技术,以便快速实施修改。

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

更新于: 2023年8月1日

239 次浏览

开启您的 职业生涯

通过完成课程获得认证

立即开始
广告