如何删除列表中的整个字典?
How do i delete a whole dictionary inside a list?
mylist = [{"a" : 1, " b" : 2}, {"c" : 1, "d" :2}]
我的清单是这样的。我将如何删除包含键 'a' 的整个字典?
您可以使用 list comprehension 创建一个带有 dict
s 而没有 'a'
键的新列表:
>>> mylist = [{"a" : 1, " b" : 2},{"c" : 1, "d" :2}]
>>> [d for d in mylist if 'a' not in d]
[{'c': 1, 'd': 2}]
如果必须从原始列表中删除元素,那么你可以这样做:
>>> mylist = [{"a" : 1, " b" : 2},{"c" : 1, "d" :2}]
# v Iterate over the copy of the list,
# v so that the change in index after the
# v deletion of elements in the list won't
# v impact the future iterations
>>> for i, d in enumerate(list(mylist)):
... if 'a' in d:
... del mylist[i]
...
>>> mylist
[{'c': 1, 'd': 2}]
你也可以用filter
来解决这个问题:
mylist = [{"a" : 1, " b" : 2}, {"c" : 1, "d" :2}]
new_list = list(filter(lambda x: "a" not in x, mylist))
输出:
[{'c': 1, 'd': 2}]
关于您最近的评论,要删除值为 "apple" 的字典:
mylist = [{"a" : "apple", "b" : "orange"}, {"c" : "pineapple", "d" : "mango"}]
final_list = list(filter(lambda x:"apple" not in x.values(), mylist))
你在评论里说了
I'm a beginner in python and would like to move slow and understand well as i go
所以我专注于解释步骤,而不是直接给你解决方案。
检查 one 字典是否包含特定键的最简单方法是使用 in
:
>>> d = {'a': 10, 'b': 20}
>>> 'a' in d
True
>>> 'c' in d
False
同样,您可以使用 not in
:
检查它是否不是字典中的键
>>> 'c' not in d
True
>>> 'b' not in d
False
由于您正在处理字典列表,因此需要对其进行迭代。使用 Python 您可以使用 for
:
遍历每个元素
>>> list_of_dicts = [{'a': 10, 'b': 20}, {'b': 10, 'c': 20}]
>>> for subdict in list_of_dicts:
... print(subdict)
...
{'a': 10, 'b': 20}
{'b': 10, 'c': 20}
所以你基本上只需要将 for
循环与检查键是否是 in
子字典结合起来。然而,修改当前正在迭代的任何内容都不是一个好主意,因此您可以创建一个新列表来存储您想要保留的词典:
>>> keep_these = []
>>> for subdict in list_of_dicts:
... if 'a' not in subdict:
... keep_these.append(subdict)
...
>>> keep_these
[{'b': 10, 'c': 20}]
在 Python 中有一种更简单的方法:列表理解。 Moinuddin Quadri 已经介绍过了,只是重复一下:
>>> [subdict for subdict in list_of_dicts if 'a' not in subdict]
[{'b': 10, 'c': 20}]
这基本上与我上面使用的 for
循环完全一样,但它更短更快。
mylist = [{"a" : 1, " b" : 2}, {"c" : 1, "d" :2}]
我的清单是这样的。我将如何删除包含键 'a' 的整个字典?
您可以使用 list comprehension 创建一个带有 dict
s 而没有 'a'
键的新列表:
>>> mylist = [{"a" : 1, " b" : 2},{"c" : 1, "d" :2}]
>>> [d for d in mylist if 'a' not in d]
[{'c': 1, 'd': 2}]
如果必须从原始列表中删除元素,那么你可以这样做:
>>> mylist = [{"a" : 1, " b" : 2},{"c" : 1, "d" :2}]
# v Iterate over the copy of the list,
# v so that the change in index after the
# v deletion of elements in the list won't
# v impact the future iterations
>>> for i, d in enumerate(list(mylist)):
... if 'a' in d:
... del mylist[i]
...
>>> mylist
[{'c': 1, 'd': 2}]
你也可以用filter
来解决这个问题:
mylist = [{"a" : 1, " b" : 2}, {"c" : 1, "d" :2}]
new_list = list(filter(lambda x: "a" not in x, mylist))
输出:
[{'c': 1, 'd': 2}]
关于您最近的评论,要删除值为 "apple" 的字典:
mylist = [{"a" : "apple", "b" : "orange"}, {"c" : "pineapple", "d" : "mango"}]
final_list = list(filter(lambda x:"apple" not in x.values(), mylist))
你在评论里说了
I'm a beginner in python and would like to move slow and understand well as i go
所以我专注于解释步骤,而不是直接给你解决方案。
检查 one 字典是否包含特定键的最简单方法是使用 in
:
>>> d = {'a': 10, 'b': 20}
>>> 'a' in d
True
>>> 'c' in d
False
同样,您可以使用 not in
:
>>> 'c' not in d
True
>>> 'b' not in d
False
由于您正在处理字典列表,因此需要对其进行迭代。使用 Python 您可以使用 for
:
>>> list_of_dicts = [{'a': 10, 'b': 20}, {'b': 10, 'c': 20}]
>>> for subdict in list_of_dicts:
... print(subdict)
...
{'a': 10, 'b': 20}
{'b': 10, 'c': 20}
所以你基本上只需要将 for
循环与检查键是否是 in
子字典结合起来。然而,修改当前正在迭代的任何内容都不是一个好主意,因此您可以创建一个新列表来存储您想要保留的词典:
>>> keep_these = []
>>> for subdict in list_of_dicts:
... if 'a' not in subdict:
... keep_these.append(subdict)
...
>>> keep_these
[{'b': 10, 'c': 20}]
在 Python 中有一种更简单的方法:列表理解。 Moinuddin Quadri 已经介绍过了,只是重复一下:
>>> [subdict for subdict in list_of_dicts if 'a' not in subdict]
[{'b': 10, 'c': 20}]
这基本上与我上面使用的 for
循环完全一样,但它更短更快。