类型错误:列表索引必须是整数或切片,而不是NoneType。

问题描述 投票:0回答:1

我有一些问题 Type Error (TypeError: list indices must be integers or slices, not NoneType)

我只是想用python做一个小的哈希表,用线性探究的方法。所以我做了一个插入方法,一个查找方法和一个哈希方法。我的插入方法有问题,这是我的代码。

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

我应该怎么做?我需要将我的表索引与None进行比较,有谁知道吗?

python hashtable typeerror nonetype
1个回答
1
投票

看起来你的self.hash函数返回的是None。因为index的值是None,所以无法找到list self.table中的值。你能把哈希函数也写出来吗?

© www.soinside.com 2019 - 2024. All rights reserved.