Python 删除具有特定值的字典键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17095163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove a dictionary key that has a certain value
提问by coderkid
I know dictionary's are not meant to be used this way, so there is no built in function to help do this, but I need to delete every entry in my dictionary that has a specific value.
我知道字典不应该以这种方式使用,因此没有内置函数可以帮助执行此操作,但是我需要删除字典中具有特定值的每个条目。
so if my dictionary looks like:
所以如果我的字典看起来像:
'NameofEntry1': '0'
'NameofEntry2': 'DNC'
...
I need to delete(probably pop) all the entries that have value DNC, there are multiple in the dictionary.
我需要删除(可能弹出)所有具有 DNC 值的条目,字典中有多个条目。
采纳答案by Ashwini Chaudhary
Modifying the original dict:
修改原始字典:
for k,v in your_dict.items():
if v == 'DNC':
del your_dict[k]
or create a new dict using dict comprehension:
或使用字典理解创建一个新的字典:
your_dict = {k:v for k,v in your_dict.items() if v != 'DNC'}
From the docson iteritems(),iterkeys()and itervalues():
从文档开始iteritems(),iterkeys()和itervalues():
Using
iteritems(),iterkeys()oritervalues()while adding or deleting entries in the dictionary may raise aRuntimeErroror fail to iterate over all entries.
使用
iteritems(),iterkeys()或itervalues()在添加或删除字典中的条目时可能会引发RuntimeError或 无法遍历所有条目。
Same applies to the normal for key in dict:loop.
这同样适用于普通for key in dict:循环。
In Python 3 this is applicable to dict.keys(), dict.values()and dict.items().
在 Python 3 中,这适用于 dict.keys(),dict.values()和dict.items()。
回答by Ankur Ankan
This should work:
这应该有效:
for key, value in dic.items():
if value == 'DNC':
dic.pop(key)
回答by Jared
You just need to make sure that you aren't modifying the dictionary while you are iterating over it else you would get RuntimeError: dictionary changed size during iteration.
您只需要确保在迭代字典时没有修改字典,否则您会得到RuntimeError: dictionary changed size during iteration.
So you need to iterate over a copy of the keys, values (for duse d.items()in 2.x or list(d.items())in 3.x)
所以,你需要遍历键,值的副本(对于d使用d.items()在2.x或list(d.items())在3.X)
>>> d = {'NameofEntry1': '0', 'NameofEntry2': 'DNC'}
>>> for k,v in d.items():
... if v == 'DNC':
... del d[k]
...
>>> d
{'NameofEntry1': '0'}
回答by Chris Johnson
If restrictions re: modifying the dictionary while iterating on it is a problem, you could create a new class compatible with dict that stores a reverse index of all keys that have the given value (updated at create / update / delete of dict item), which can be arguments to del without iterating over the dict's items.
如果限制 re:在迭代时修改字典是一个问题,您可以创建一个与 dict 兼容的新类,该类存储具有给定值的所有键的反向索引(在 dict 项目的创建/更新/删除时更新),这可以是 del 的参数,而无需迭代 dict 的项目。
Subclass dict, and override __setitem__, __delitem__, pop, popitemand clear.
子类快译通,和覆盖__setitem__,__delitem__,pop,popitem和clear。
If this is an operation you're doing a lot of, that might be convenient and fast.
如果这是您经常进行的操作,那可能会方便快捷。

