python - 具有相同哈希值的不同python frozensets

标签 python set frozenset

我的理解是,对需要包含可哈希对象的两个不同的 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/

相关文章:

python - 关于python中的def和return函数

python - MousePressEvent,QGraphicsView中的位置偏移

python - 如果在 python 中成功,将元素添加到集合中会返回 true 吗?

python - 在 Python 中检查两个 frozensets 是否相等的时间复杂度

python - 从 python 中的 freezeset 访问项目

Python:frozensets 比较

python - 考虑到python中的一些条件,如何在字符串中插入一个字符

python - 嵌入Python 3.3

algorithm - 相交集使得结果是一组具有共同唯一元素的集合

python - 比较集合的交集并返回包含属于最大交集的集合的字典键的最快方法