一个Python字典是一个散列表的例子吗? [英] Is a Python dictionary an example of a hash table?

查看:445
本文介绍了一个Python字典是一个散列表的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

是的,它是一个哈希映射或哈希表。您可以阅读由蒂姆·彼得斯(Tim Peters)编写的python's dict实现的描述,这里

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.

这就是为什么你不能使用某些不可哈希作为dict键,如列表: / p>

That's why you can't use something 'not hashable' as a dict key, like a list:

>>> 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

您可以了解有关哈希表的更多信息检查它是如何在python中实现的,而为什么以这种方式实现

这篇关于一个Python字典是一个散列表的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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