python - 类型错误 - 不可散列的类型列表 - 方法调用之前
问题描述
因此,我正在开发编程问题网站,您可以在其中使用名为 solution 的类提交编程问题的答案。您使用网站使用的特殊命名方法提交代码,以查看您是否能够回答问题。
我向网站提交了以下代码来解决一个问题,该问题涉及计算遍历包含许多障碍的矩阵的方法的数量:
from functools import cache
class Solution:
def uniquePathsWithObstacles(self, obstacleGrid: List[List[int]]) -> int:
start = (0,0)
return self.uniquePathsHelper(obstacleGrid,start)
@cache
def uniquePathsHelper(self, matrix, pt):
neighbors = self.get_neighbors(matrix,pt)
if not neighbors:
return 0
elif (len(matrix) - 1, len(matrix[0]) - 1) in neighbors:
return 1
else:
total = 0
for next_pt in neighbors:
total += self.uniquePathsHelper(matrix,next_pt)
return total
def get_neighbors(self,matrix,pt):
neighbors = []
shifts = [(0,1), (1,0)]
max_col = len(matrix[0]) - 1
max_row = len(matrix) - 1
for row_shift, col_shift in shifts:
row, col = row_shift + pt[0], col_shift + pt[1]
if row > max_row or row < 0:
pass
elif col > max_col or col < 0:
pass
elif matrix[row][col] == 1:
pass
else:
neighbors.append((row, col))
return neighbors
我得到一个非常奇怪的错误:
TypeError: unhashable type: 'list'
return self.uniquePathsHelper(obstacleGrid,start)
Line 8 in uniquePathsWithObstacles (Solution.py)
ret = Solution().uniquePathsWithObstacles(param_1)
Line 89 in _driver (Solution.py)
_driver()
Line 100 in <module> (Solution.py)
我了解什么是不可散列的类型错误,但我看不到我试图在 uniquePathsWithObstacles 中对列表进行散列。更奇怪的是,该程序似乎永远无法访问 uniquePathsHelper,这是唯一可以做任何事情的函数。
我想知道 Python 的对象系统是否存在问题以及方法允许的内容。
有谁知道这里可能出了什么问题?
解决方案
推荐阅读
- swift - 如何仅使用 CNContactViewController 来创建新联系人?(迅速)
- r - “显着”和“不显着”之间的区别是否显着?
- c++ - std::ofstream 不写入文件
- r - 找不到功能 %>%
- java - Java中的自定义异常处理,如何只在异常对象中打印出我的参数而不调用外部类?
- php - 如何在php中循环数据
- javascript - 谷歌标签管理器在 wordpress 网站上导致 javascript 错误
- c++ - CMake: *.hpp 和 *.cpp 在 target_include_*
- reactjs - 如何处理具有对象属性的数组以生成反应元素
- javascript - 里程表无法仅在 OBS 中加载