python - Python 字典是哈希表的一个例子吗?

标签 python hash dictionary hashmap hashtable

Python 中的基本数据结构之一是字典,它允许记录“键”以查找任何类型的“值”。这是在内部作为哈希表实现的吗?如果不是,那是什么?

最佳答案

是的,它是一个 HashMap 或哈希表。您可以阅读 Tim Peters 所写的 python dict 实现的描述,here .

这就是为什么你不能使用“不可散列”的东西作为字典键的原因,比如列表:

>>> a = {}
>>> b = ['some', 'list']
>>> hash(b)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list objects are unhashable
>>> a[b] = 'some'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list objects are unhashable

您可以read more about hash tablescheck how it has been implemented in pythonwhy it is implemented that way .

关于python - Python 字典是哈希表的一个例子吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/114830/

相关文章:

python - 奇怪的字典迭代顺序

python - 如何使用带有 Python 的 Selenium Webdriver 在页面上有多个图表的选择器中获取正确的 highchart 数字?

python - C++ std::vector 乘法中是否存在已知的不一致行为?

python - 如何通过检查 python 列表是否与给定模式匹配来对列表进行排序?

Javascript Sha256 与 PHP Sha256 : Line breaks create different hash?

perl - Perl 脚本中的 HASH(0x1970c80)

c++ - std::map,自定义比较器的设计约束

python - 如何在 Django 中拥有多个 AUTH_USER_MODEL

python - 在Python中更改视频帧速率(fps)

java - 自定义哈希算法