python - TypeError:列表索引必须是整数或切片,而不是 NoneType
问题描述
我有一些麻烦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.hash 函数正在返回 None。由于 index 的值为 None 它无法在列表 self.table 中找到该值。你也可以写散列函数吗?
推荐阅读
- node.js - 对于来自 mongo 数据库的循环节点 js
- python - 找不到 Pyinstaller 库:libzmq-v141-mt-4_3_4-97a8c0cb.dll 依赖于 lib\site-packages\zmq\backend\cython\_proxy_steerable.cp39-win_amd64.pyd
- powerbi - 在 Power BI Desktop 中按项目和日期计算更改
- angular - 全选复选框不应选择禁用的复选框记录角度
- c++ - 调用继承参数包的模板构造函数
- r - 找不到功能 dtwclust R
- amazon-web-services - 如何删除失败的 .ebextensions 脚本?
- angular - ionic : 离子后退按钮上的清除文本字段
- javascript - 如何设置一个逻辑以从给定代码呈现组件
- angular11 - core.js:6210 错误类型错误:无法读取 null 的属性“writeValue”