为什么dict中的key和dict.keys()中的key具有相同的输出? [英] Why do `key in dict` and `key in dict.keys()` have the same output?

查看:103
本文介绍了为什么dict中的key和dict.keys()中的key具有相同的输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在字典中搜索键,但是我忘记添加keys()函数.我仍然得到了预期的答案.

I tried to search the keys in a dictionary, but I forgot to add the keys() function. I still got the expected answer.

为什么这两个表达式的结果相同?

Why is the result the same for these two expressions?

key in dict

key in dict.keys()

推荐答案

要理解为什么key in dct返回与key in dct.keys()相同的结果,需要回顾一下过去.从历史上看,在Python 2中,可以使用 Python 2.2 时,变成key in dct,基本上可以完成相同的操作:

To understand why key in dct returns the same result as key in dct.keys() one needs to look in the past. Historically in Python 2, one would test the existence of a key in dictionary dct with dct.has_key(key). This was changed for Python 2.2, when the preferred way became key in dct, which basically did the same thing:

在一个较小的相关更改中,in运算符现在可用于词典,因此key in dict现在等同于dict.has_key(key)

In a minor related change, the in operator now works on dictionaries, so key in dict is now equivalent to dict.has_key(key)

in的行为是根据__contains__ dunder方法在内部实现的. Python语言参考-3数据模型中记录了它的行为. :

The behaviour of in is implemented internally in terms of the __contains__ dunder method. Its behaviour is documented in the Python language reference - 3 Data Model:

object.__contains__(self, item)

被调用以实施成员资格测试操作员.如果item位于self中,则应返回true,否则返回false. 对于映射对象,这应该考虑映射的键而不是值或键-项对. 对于未定义__contains__()的对象,成员资格测试首先通过__iter__()尝试迭代,然后通过__getitem__()尝试旧的序列迭代协议,请参见语言参考中的本节.

Called to implement membership test operators. Should return true if item is in self, false otherwise. For mapping objects, this should consider the keys of the mapping rather than the values or the key-item pairs. For objects that don’t define __contains__(), the membership test first tries iteration via __iter__(), then the old sequence iteration protocol via __getitem__(), see this section in the language reference.

(重点是我的; Python中的词典是映射对象)

(emphasis mine; dictionaries in Python are mapping objects)

Python 3 中,has_key完全删除了该方法,现在有 正确的方法来测试密钥是否存在,如所记录的那样,完全是key in dict.

In Python 3, the has_key method was removed altogether and now there the correct way to test for the existence of a key is solely key in dict, as documented.

与上面的2相比,key in dct.keys()从来不是测试字典中是否存在键的正确方法. 两个示例的结果确实相同,但是key in dct.keys()在Python 3上稍慢,而在Python 2上则慢得多.

In contrast with the 2 above, key in dct.keys() has never been the correct way of testing whether a key exists in a dictionary. The result of both your examples is indeed the same, however key in dct.keys() is slightly slower on Python 3 and is abysmally slow on Python 2.

key in dct返回true-不管是两个键还是一百万个键都没有关系-其时间复杂度平均是恒定的大小写( O(1))

key in dct returns true, if the key is found as a key in the dct in almost constant time operation - it does not matter whether there are two or a million keys - its time complexity is constant on average case (O(1))

dct.keys()创建所有键的list;在Python 3中, view 钥匙这两个对象都可以理解key in x.在Python 2中,它的作用类似于任何 iterable ;一个值等于给定值(此处为key)时,将对这些值进行迭代并返回True.

dct.keys() in Python 2 creates a list of all keys; and in Python 3 a view of keys; both of these objects understand the key in x. With Python 2 it works like for any iterable; the values are iterated over and True is returned as soon as one value is equal to the given value (here key).

在实践中,在Python 2中,您会发现key in dct.keys()key in dict慢得多(key in dct.keys()与键的数量成线性比例-其时间复杂度为 O(n)-生成所有键列表的dct.keys() key in key_list 均为 O(n))

In practice, in Python 2 you'd find key in dct.keys() much slower than key in dict (key in dct.keys() scales linearly with the number of keys - its time complexity is O(n) - both dct.keys(), which builds a list of all keys, and key in key_list are O(n))

在Python 3中,key in dct.keys()不会比key in dct慢很多,就像 视图 不会列出密钥,并且访问权限仍为 O(1),但是实际上至少要慢一个常数,并且还要多7个字符,因此即使在Python 3上,也几乎没有理由使用它.

In Python 3, the key in dct.keys() won't be much slower than key in dctas the view does not make a list of the keys, and the access still would be O(1), however in practice it would be slower by at least a constant value, and it is 7 more characters, so there is usually practically no reason to use it, even if on Python 3.

这篇关于为什么dict中的key和dict.keys()中的key具有相同的输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆