为什么两个不同对象上的哈希函数返回相同的值? [英] Why hash function on two different objects return same value?

查看:217
本文介绍了为什么两个不同对象上的哈希函数返回相同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了Spyder,运行Python 2.7.

I used Spyder, run Python 2.7.

刚发现有趣的事情:

  1. hash(-1)和hash(-2)都返回-2,有问题吗?我虽然在不同对象上的哈希函数应返回不同的值.我读过以前的文章,将-1保留为Python中的错误.
  2. hash('s')返回1835142386,然后hash(1835142386)返回相同的值.这是另一个问题吗?

谢谢.

推荐答案

-1在Python中不是保留为错误".不知道那将意味着什么.如果不允许使用-1,则有很多程序无法简单明了地编写.

-1 is not "reserved as an error" in Python. Not sure what that would even mean. There are a huge number of programs you couldn't write simply and clearly if you weren't allowed to use -1.

有问题吗?"不需要.哈希函数不需要为每个对象返回不同的哈希.实际上,这是不可能的,因为存在的对象多于散列的对象. CPython的hash()具有很好的属性,可以返回非负数的参数,直到sys.maxint,这就是为什么在第二个问题hash(hash('s')) == hash('s')中,但这是一个实现细节.

"Is there a problem?" No. Hash functions do not need to return a different hash for every object. In fact, this is not possible, since there are many more possible objects than there are hashes. CPython's hash() has the nice property of returning its argument for non-negative numbers up to sys.maxint, which is why in your second question hash(hash('s')) == hash('s'), but that is an implementation detail.

-1和-2具有相同的哈希的事实仅意味着,将这些值用作字典键将导致哈希冲突.哈希冲突是预期的情况,将由Python自动解决,添加的第二个键将简单地放在字典中的下一个可用插槽中.这样,访问第二个插入的键将比访问另一个键稍慢,但是在大多数情况下,访问权限不会比您注意到的慢.

The fact that -1 and -2 have the same hash simply means that using those values as, for example, dictionary keys will result in a hash conflict. Hash conflicts are an expected situation and are automatically resolved by Python, and the second key added would simply go in the next available slot in the dictionary. Accessing the key that was inserted second would then be slightly slower than accessing the other one, but in most cases, not enough slower that you'd notice.

可能使用相同的哈希值构造大量不相等的对象,当将它们存储在字典或集合中时,这将导致容器的性能大大下降,因为添加的每个对象都会导致哈希碰撞,但是除非您寻找它,否则您不会碰到它.

It is possible to construct a huge number of unequal objects all with the same hash value, which would, when stored in a dictionary or a set, cause the performance of the container to deteriorate substantially because every object added would cause a hash collision, but it isn't something you will run into unless you go looking for it.

这篇关于为什么两个不同对象上的哈希函数返回相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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