微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在dict.keys()中dict和key之间的键有什么区别?

我最近遇到了 python 2.7的问题:

class A(object):
    def __init__(self,v):
        self.v = v

    def __eq__(self,other):
        return self.v == other.v

a1 = A(1)
a2 = A(1)

所以:

print a1 == a2  # True

和:

d = {a1: 1}
print a2 in d.keys()  # True

但:

print a2 in d  # False

问题是a2 ind.keys()和d中的a2之间的主要区别是什么?我怎样才能获得d中的a2是真的?

解决方法

在Python 2.7中,dict.keys返回一个键列表,而d.keys()中的a2将线性迭代所有键,以查找a2是否在列表中.

但是d中的a2将只是在字典中基于对象a2的哈希值进行O(1)查找,以查看密钥a2是否在d中.

但在你的情况下,问题完全不同.引用official documentation,

If a class does not define a __cmp__() or __eq__() method it should not define a __hash__() operation either; if it defines __cmp__() or __eq__() but not __hash__(),its instances will not be usable in hashed collections. If a class defines mutable objects and implements a __cmp__() or __eq__() method,it should not implement __hash__(),since hashable collection implementations require that a object’s hash value is immutable (if the object’s hash value changes,it will be in the wrong hash bucket).

由于您没有明确定义__hash__函数,因此您打破了它们之间的契约,__ hash__使用基于对象id认散列,对于a1和a2都是不同的.因此,即使a1和a2相似,哈希对象也会将它们视为两个不同的对象,因为它们的哈希值不同.

解决这个问题,你需要定义__hash__函数,就像这样

class A(object):

    def __init__(self,other):
        return self.v == other.v

    def __hash__(self):
        return hash(self.v)

a1 = A(1)
a2 = A(1)

d = {a1: 1}
print a2 in d.keys()  # True
print a2 in d         # True

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐