小编and*_*fun的帖子

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

我最近遇到了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)
Run Code Online (Sandbox Code Playgroud)

所以:

print a1 == a2  # True
Run Code Online (Sandbox Code Playgroud)

和:

d = {a1: 1}
print a2 in d.keys()  # True
Run Code Online (Sandbox Code Playgroud)

但:

print a2 in d  # False
Run Code Online (Sandbox Code Playgroud)

问题是a2 ind.keys()和之间的主要区别是什么a2 in d?我该怎么a2 in d is True办?

python dictionary key python-2.7

3
推荐指数
1
解决办法
102
查看次数

标签 统计

dictionary ×1

key ×1

python ×1

python-2.7 ×1