【问题标题】:TypeError: list indices must be integers or slices, not NoneTypeTypeError:列表索引必须是整数或切片,而不是 NoneType
【发布时间】: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


【解决方案1】:

看起来您的 self.hash 函数返回 None。由于 index 的值为 None 它无法在列表 self.table 中找到值。也可以写散列函数吗?

【讨论】:

  • 是的,我只是在我的哈希函数中出了点小错误。谢谢!
猜你喜欢
  • 2016-09-16
  • 1970-01-01
  • 2019-07-04
  • 2015-12-09
  • 2021-11-28
  • 2020-09-04
  • 2019-10-01
相关资源
最近更新 更多