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'}]
結論
在處理大量資料時,更改資料是流程中的必要步驟。因此,必須瞭解各種技術,以便快速實施修改。
本文詳細介紹了從資料來源中包含的字典列表中刪除字典的每種可能方法。在使用這種方法時,您必須注意,因為可能會出現資料錯誤,這可能導致資料丟失。因此,在實施任何更改之前,必須備份您的資料。
廣告