TypeError: list indices must be integers or slices, not NoneType
TypeError: list indices must be integers or slices, not NoneType
我在使用 Type Error (TypeError: list indices must be integers or slices, not NoneType)
时遇到了一些问题
我只想在 python 中用线性探测做一点散列 table。因此,到目前为止,我曾经制作过一个插入方法,一个查找方法和一个散列方法。我的插入方法有问题。
这是我的代码
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":
我该怎么办?
我需要将我的 table 索引与 None 进行比较。
有人有想法吗?
您的 self.hash 函数似乎正在返回 None。由于索引的值为 None,因此无法在列表 self.table 中找到该值。你也会写哈希函数吗?
我在使用 Type Error (TypeError: list indices must be integers or slices, not NoneType)
我只想在 python 中用线性探测做一点散列 table。因此,到目前为止,我曾经制作过一个插入方法,一个查找方法和一个散列方法。我的插入方法有问题。 这是我的代码
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":
我该怎么办? 我需要将我的 table 索引与 None 进行比较。 有人有想法吗?
您的 self.hash 函数似乎正在返回 None。由于索引的值为 None,因此无法在列表 self.table 中找到该值。你也会写哈希函数吗?