我的理解是,对需要包含可哈希对象的两个不同的 frozensets(不可变的 Python 集)进行哈希处理应该会导致两个不同的哈希值。为什么我会得到以下两个不同 frozenset 的输出?
In [11]: a
Out[11]: frozenset({(2, -2), (2, -1), (3, -2), (3, -1)})
In [12]: b
Out[12]: frozenset({(4, -2), (4, -1), (5, -2), (5, -1)})
In [13]: hash(a)
Out[13]: 665780563440688
In [14]: hash(b)
Out[14]: 665780563440688
最佳答案
您似乎偶然发现了两个具有相同哈希码和不同内容的 frozenset
。这并不像看起来那么奇怪,因为哈希码的属性是保证它们对于相等的对象是相等的,并且对于不相等的对象可能不同。
来自 Python 文档:
hash(object) -> integer
Return a hash value for the object. Two objects with the same value have the same hash value. The reverse is not necessarily true, but likely.
绝对最简单的例子是数字 -1
和 -2
,它们在 python 中具有相同的哈希码:
>>> print(hash(-1))
-2
>>> print(hash(-2))
-2
关于python - 具有相同哈希值的不同python frozensets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29958751/