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

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 tables或 check how it has been implemented in python和 why it is implemented that way .

https://stackoverflow.com/questions/114830/

相关文章:

linux - 如何测量进程的单独 CPU 核心使用率?

python - NumPy 2d 数组的切片,或者如何从 nxn 数组 (n>m) 中提取 mxm

python - TransactionManagementError "You can' t 在使

linux - 防止 strace 缩写参数?

c - 将简单的套接字变成 SSL 套接字

python - 将 Pandas 列转换为 DateTime

python - 如何修改文本文件?

linux - ssh:无法解析主机名 [主机名]:提供节点名或服务名,或未知

python - 错误 "Microsoft Visual C++ 14.0 is required

c - 错误 : Libtool library used but 'LIBTOOL' is und