首页 > 解决方案 > 类型错误 - 不可散列的类型列表 - 方法调用之前

问题描述

因此,我正在开发编程问题网站,您可以在其中使用名为 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 的对象系统是否存在问题以及方法允许的内容。

有谁知道这里可能出了什么问题?

标签: pythonlistobjecttypeerrorhashable

解决方案


推荐阅读