这个问题的答案是社区的努力。编辑现有答案以改进这篇文章。它目前不接受新的答案或交互。
我想在更新键的值之前测试一个键是否存在于字典中。我写了以下代码:
if 'key1' in dict.keys():
print "blah"
else:
print "boo"
我认为这不是完成这项任务的最佳方式。有没有更好的方法来测试字典中的键?
dict.keys()
会创建一个键列表,但如果此模式没有针对在严肃实现中转换为 if 'key1' in dict:
进行优化,我会感到惊讶。
x in dict.keys()
来检查键。发生这种情况是因为在 Java 中迭代键的常用方法是 {2 },这个习惯导致 for k in dict.keys()
感觉比 for k in dict
更自然(在性能方面应该仍然没问题?),但是检查键也变成 if k in dict.keys()
,这是一个问题......
if k in dict_:
测试 dict_ 的 KEYS 中是否存在 k,因此您仍然不需要 dict_.keys()
。 (这让我很生气,因为它读起来就像它在 dict 中测试 value 一样。但事实并非如此。)
d = {"key1": 10, "key2": 23}
if "key1" in d:
print("this will execute")
if "nonexistent key" in d:
print("this will not")
当键不存在时,使用 dict.get()
提供默认值:
d = {}
for i in range(10):
d[i] = d.get(i, 0) + 1
要为 每个 键提供默认值,请在每个分配上使用 dict.setdefault()
:
d = {}
for i in range(10):
d[i] = d.setdefault(i, 0) + 1
或使用 collections
模块中的 defaultdict
:
from collections import defaultdict
d = defaultdict(int)
for i in range(10):
d[i] += 1
您不必调用密钥:
if 'key1' in dict:
print("blah")
else:
print("boo")
这将是很多 faster,因为它使用字典的散列而不是进行线性搜索,调用键会这样做。
您可以使用 in 关键字测试字典中是否存在键:
d = {'a': 1, 'b': 2}
'a' in d # <== evaluates to True
'c' in d # <== evaluates to False
在改变字典之前检查字典中键是否存在的一个常见用途是默认初始化值(例如,如果您的值是列表,并且您希望确保有一个空列表可以追加插入键的第一个值时)。在这种情况下,您可能会发现 collections.defaultdict()
类型很有趣。
在旧代码中,您可能还会发现 has_key()
的一些用途,这是一种用于检查字典中是否存在键的已弃用方法(只需使用 key_name in dict_name
)。
您可以将代码缩短为:
if 'key1' in my_dict:
...
然而,这充其量只是外观上的改进。为什么你认为这不是最好的方法?
有关 accepted answer's 建议方法(10 M 循环)的速度执行的更多信息:
mydict 中的“key”经过时间 1.07 秒
mydict.get('key') 经过时间 1.84 秒
mydefaultdict['key'] 经过时间 1.07 秒
因此,建议对 get
使用 in
或 defaultdict
。
get
的 1.84s 是 < 1.07*2 ;-P
我建议改用 setdefault
方法。听起来它会做你想做的一切。
>>> d = {'foo':'bar'}
>>> q = d.setdefault('foo','baz') #Do not override the existing key
>>> print q #The value takes what was originally in the dictionary
bar
>>> print d
{'foo': 'bar'}
>>> r = d.setdefault('baz',18) #baz was never in the dictionary
>>> print r #Now r has the value supplied above
18
>>> print d #The dictionary's been updated
{'foo': 'bar', 'baz': 18}
setdefault
与 OP 的问题有什么关系?
Python 中的字典有一个 get('key', default)
方法。所以你可以设置一个默认值,以防没有任何键。
values = {...}
myValue = values.get('Key', None)
get
方法的第二个参数是可选的,如果不包含则默认为 None
,因此 values.get('Key', None)
与 values.get('Key')
相同。
使用 EAFP(请求宽恕比请求许可更容易):
try:
blah = dict["mykey"]
# key exists in dict
except KeyError:
# key doesn't exist in dict
查看其他堆栈溢出帖子:
在 Python 中使用“try”与“if”
在 Python 中检查成员是否存在
检查给定键是否已存在于字典中
为了了解如何做到这一点,我们首先检查我们可以在字典上调用哪些方法。
以下是方法:
d={'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}
Python Dictionary clear() Removes all Items
Python Dictionary copy() Returns Shallow Copy of a Dictionary
Python Dictionary fromkeys() Creates dictionary from given sequence
Python Dictionary get() Returns Value of The Key
Python Dictionary items() Returns view of dictionary (key, value) pair
Python Dictionary keys() Returns View Object of All Keys
Python Dictionary pop() Removes and returns element having given key
Python Dictionary popitem() Returns & Removes Element From Dictionary
Python Dictionary setdefault() Inserts Key With a Value if Key is not Present
Python Dictionary update() Updates the Dictionary
Python Dictionary values() Returns view of all values in dictionary
检查密钥是否已存在的残酷方法可能是 get()
方法:
d.get("key")
另外两个有趣的方法 items()
和 keys()
听起来工作量太大。因此,让我们检查一下 get()
是否适合我们。我们有我们的字典 d
:
d= {'clear':0, 'copy':1, 'fromkeys':2, 'get':3, 'items':4, 'keys':5, 'pop':6, 'popitem':7, 'setdefault':8, 'update':9, 'values':10}
打印显示我们没有的密钥将返回 None
:
print(d.get('key')) #None
print(d.get('clear')) #0
print(d.get('copy')) #1
如果密钥存在或不存在,我们使用它来获取信息。但是如果我们创建一个带有单个 key:None
的字典,请考虑这一点:
d= {'key':None}
print(d.get('key')) #None
print(d.get('key2')) #None
如果某些值可能是 None
,则导致该 get()
方法不可靠。
这个故事应该有一个更幸福的结局。如果我们使用 in
比较器:
print('key' in d) #True
print('key2' in d) #False
我们得到正确的结果。
我们可以检查 Python 字节码:
import dis
dis.dis("'key' in d")
# 1 0 LOAD_CONST 0 ('key')
# 2 LOAD_NAME 0 (d)
# 4 COMPARE_OP 6 (in)
# 6 RETURN_VALUE
dis.dis("d.get('key2')")
# 1 0 LOAD_NAME 0 (d)
# 2 LOAD_METHOD 1 (get)
# 4 LOAD_CONST 0 ('key2')
# 6 CALL_METHOD 1
# 8 RETURN_VALUE
这表明 in
比较运算符不仅更可靠,而且比 get()
更快。
.get()
可以为 default
值提供第二个参数,可用于处理 key:None
的问题。示例:d.get("key", False)
.get()
是最快的方法。另一种选择是在 try
/except
块中分配
获得结果的方法有:
if your_dict.has_key(key) 在 Python 3 中被移除
如果键入 your_dict
尝试/排除块
哪个更好取决于三件事:
字典是“通常有钥匙”还是“通常没有钥匙”。您是否打算使用 if...else...elseif...else 之类的条件?字典有多大?
阅读更多:http://paltman.com/try-except-performance-in-python-a-simple-test/
使用 try/block 代替 'in' 或 'if':
try:
my_dict_of_items[key_i_want_to_check]
except KeyError:
# Do the operation you wanted to do for "key not present in dict".
else:
# Do the operation you wanted to do with "key present in dict."
仅限 Python 2:(并且 Python 2.7 已经支持 `in`)
您可以使用 has_key()
方法:
if dict.has_key('xyz')==1:
# Update the value for the key
else:
pass
只是添加到 Chris. B's (best) answer 的 FYI:
d = defaultdict(int)
也可以;原因是调用 int()
返回 0
,这是 defaultdict
在幕后所做的(在构造字典时),因此文档中的名称为“Factory Function”。
defaultdict(lambda: 0)
而不是 defaultdict(int)
因为我认为发生了什么更清楚;如果您在没有参数的情况下调用 int()
,则读者不需要知道您会得到 0
。 YMMV。
Python 字典具有称为 __contains__
的方法。如果字典有键,此方法将返回 True,否则返回 False。
>>> temp = {}
>>> help(temp.__contains__)
Help on built-in function __contains__:
__contains__(key, /) method of builtins.dict instance
True if D has a key k, else False.
__contains__
是非常糟糕的做法。正确的做法是使用 in
运算符,即调用 __contains__
函数的 containment check
。
foo = x['foo'] if x.__contains__('foo') else 'bar'
。任何想法如何将 in
运算符用作此表达式的一部分?
foo = x['foo'] if 'foo' in x else 'bar'
另一种使用布尔运算符检查键是否存在的方法:
d = {'a': 1, 'b':2}
keys = 'abcd'
for k in keys:
x = (k in d and 'blah') or 'boo'
print(x)
这返回
>>> blah
>>> blah
>>> boo
>>> boo
解释
首先,您应该知道,在 Python 中,0
、None
或长度为零的对象的计算结果为 False
。其他所有内容的计算结果为 True
。布尔运算从左到右求值并返回操作数不是 True 或 False。
让我们看一个例子:
>>> 'Some string' or 1/0
'Some string'
>>>
由于 'Some string'
的计算结果为 True
,因此 or
的其余部分不会被计算,并且不会引发除以零错误。
但是如果我们切换顺序 1/0
会首先被评估并引发异常:
>>> 1/0 or 'Some string'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: division by zero
>>>
我们可以使用这个模式来检查一个键是否存在。
(k in d and 'blah')
和
if k in d:
'blah'
else:
False
如果密钥存在,这已经返回正确的结果,但我们希望它在不存在时打印“boo”。因此,我们获取结果并使用 'boo'
or
>>> False or 'boo'
'boo'
>>> 'blah' or 'boo'
'blah'
>>>
您可以使用 for
循环遍历字典并获取要在字典中查找的键的名称。之后,使用 if
条件检查它是否存在:
dic = {'first' : 12, 'second' : 123}
for each in dic:
if each == 'second':
print('the key exists and the corresponding value can be updated in the dictionary')
it is exist
和 not exist
不定期副业成功案例分享
get
。使用in
and 将项目从字典中拉出是没有意义的。in
是最好的方法。0
,则 get 是一个不好的测试。很难学到这一点:/