首頁 >後端開發 >Python教學 >Python - 從字典清單中刪除字典

Python - 從字典清單中刪除字典

WBOY
WBOY轉載
2023-08-19 21:49:041294瀏覽

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

字典是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中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除