【发布时间】:2013-12-07 23:48:40
【问题描述】:
Hash tables are supposed be high-performance mappings、and because Python dicts are implemented with hash tables,它们的性能也很高。但是我在查看负整数的哈希值时遇到了一个奇怪的结果。
>>> for i in range(7):
... print hash(i-4)
...
-4
-3
-2
-2
0
1
2
但这显然对dicts没有影响:
>>> d = dict()
>>> d[-1] = 'foo'
>>> d[-2] = 'bar'
>>> d
{-2: 'bar', -1: 'foo'}
为什么会发生这种情况,为什么我使用 dicts 时不受影响?
【问题讨论】:
标签: python dictionary hash language-implementation python-internals