One of the basic data structures in Python is the dictionary, which allows one to record "keys" for looking up "values" of any type. Is this implemented internally as a hash table? If not, what is it?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
There must be more to a Python dictionary than a table lookup on hash(). By brute experimentation I found this hash collision:
Yet it doesn't break the dictionary:
Sanity check:
Possibly there's another lookup level beyond hash() that avoids collisions between dictionary keys. Or maybe dict() uses a different hash.
(By the way, this in Python 2.7.10. Same story in Python 3.4.3 and 3.5.0 with a collision at
hash(1.1) == hash(214748749.8)
.)Yes, it is a hash mapping or hash table. You can read a description of python's dict implementation, as written by Tim Peters, here.
That's why you can't use something 'not hashable' as a dict key, like a list:
You can read more about hash tables or check how it has been implemented in python and why it is implemented that way.
Yes. Internally it is implemented as open hashing based on a primitive polynomial over Z/2 (source).
To expand upon nosklo's explanation: