您好,欢迎访问一九零五行业门户网

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

字典是python中常用的功能,用于根据用户的需要存储数据。另一个典型的过程涉及编辑或操作这些数据。要成为一个高效和快速的程序员,你必须弄清楚如何从字典列表中删除一个字典。本文将介绍从字典列表中删除字典的许多技巧。
从字典列表中删除字典的不同方法循环方法我们将指定要从字典列表中删除的字典,然后我们将使用if()创建一个条件来提供一个参数,以从字典列表中删除该字典。我们可以通过以下示例更清楚地理解:
example 的中文翻译为:示例# dictionariescities = [ {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 removedfor 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 usedprint(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 的中文翻译为:示例#dictionariescities = [ {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 removedcities = [city for city in cities if city[city] != remove] #creating a new list and specifying the condition to remove the unwanted dictionaryprint(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 的中文翻译为:示例# dictionariescities = [ {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 englandprint(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 的中文翻译为:示例#dictionariescities = [ {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 dictionariesprint(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 的中文翻译为:示例#dictionariescities = [ {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 0del cities[dictionary_remove] #it commands to delete the dictionary in specified index numberprint(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 - 从字典列表中删除字典的详细内容。
其它类似信息

推荐信息