类型错误 - 不可散列类型列表 - 方法调用前

时间:2021-04-13 23:19:57

标签: python list object typeerror hashable

所以,我正在开发编程问题网站,您可以在该网站上使用称为解决方案的类提交编程问题的答案。您使用一种特殊命名的方法提交代码,网站会使用该方法查看您是否能够回答问题。

我向网站提交了以下代码以解决一个问题,该问题涉及计算遍历包含许多障碍的矩阵的方法数:

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 的对象系统是否存在问题,以及方法上允许的内容。

有人知道这里有什么问题吗?

0 个答案:

没有答案