【发布时间】:2020-09-23 08:24:33
【问题描述】:
Type Error (TypeError: list indices must be integers or slices, not NoneType) 有一些问题
我只想在 python 中做一个带有线性探测的小哈希表。因此,到目前为止,我曾经制作过 insert 方法、find 方法和 hash 方法。我的插入方法有问题。 这是我的代码
def insert(self, key):
index = self.hash(key)
count = 0
if self.table[index] == None or self.table[index].getValue() == "DELETED":
self.table[index] = key
return count + 1
else:
inserted = False
while inserted != True:
index += 1
count += 1
if index == self.size:
index = 0
if self.table[index] == None or self.table[index].getValue() == "DELETED":
self.table[index] = key
return count
问题出在这两行
if self.table[index] == None or self.table[index].getValue() == "DELETED":
我该怎么办? 我需要将我的表索引与无进行比较。 有人有想法吗?
【问题讨论】:
-
self.table是一个列表吗?您可以使用None作为字典的键,但它不是有效的列表索引。 -
你能把你的
self.hash函数贴在这里吗? -
table是什么? -
来自我的
def __init__(self, size = 11)方法self.table = [None] * self.size所以表将成为我的哈希表
标签: python hashtable typeerror nonetype