在python中获取最短路径时发生Dijkstra算法错误

在python中获取最短路径时发生Dijkstra算法错误,python,json,python-3.x,file,Python,Json,Python 3.x,File,我编写代码是为了显示json文件的最短路径,但我一直收到一个回溯错误,我想这与我的参数有关,主要是dijkstas算法所在的位置。如果是这样的话,我应该使用什么参数,或者我是否偏离了这些参数中应该使用的参数? 我正在加载json文件的Main with open("distanceTables.json", 'r') as f: var1 = json.load(f) visited = chainingHashMap()

我编写代码是为了显示json文件的最短路径,但我一直收到一个回溯错误,我想这与我的参数有关,主要是dijkstas算法所在的位置。如果是这样的话,我应该使用什么参数,或者我是否偏离了这些参数中应该使用的参数? 我正在加载json文件的Main

       with open("distanceTables.json", 'r') as f:
            var1 = json.load(f)
            visited = chainingHashMap()
            path =[]

        dijkstrasShortestDistance(path, var1, visited

)
我为最短路径编写的算法需要三个参数和返回距离

    def dijkstrasShortestDistance(start, end, graph):
        visited = chainingHashMap()

        paths = list()
        paths.append(([start], 0))
        while len(paths) > 0:
            nowPath = paths.pop()
            finalItem = nowPath[0][-1]
            hasVisited = visited.get(finalItem)[0]
            if not hasVisited:
                visited.insert(finalItem, True)
            else:
                continue
            if finalItem == end:
                return nowPath
            nextTuple = graph.get(finalItem)
            if not nextTuple[0]:
                continue
            nextNodes = nextTuple[1]
            for nextNodes in nextNodes:
                newPath = list(nowPath[0])
                newPath.append(nextNodes[0])
                newDistance = nowPath[1]
                newDistance += nextNodes[1]
                paths.append((newPath, newDistance))
            paths = sorted(paths, key=lambda tuple_item: tuple_item[0])
        return (list(), 0)
im使用的JSON文件包含每个位置和距离

    "Taylorsville-Bennion Heritage City Gov Off 1488 4800 S": {
            "Western Governors University 4001 South 700 East, Salt Lake City, UT 84107": 11.0,
            "International Peace Gardens 1060 Dalton Ave S": 6.4,
            "Sugar House Park 1330 2100 S": 9.2,
            "Salt Lake City Division of Health Services 177 W Price Ave": 5.6,
            "South Salt Lake Public Works 195 W Oakland Ave": 6.9,
            "Salt Lake City Streets and Sanitation 2010 W 500 S": 8.6,
            "Deker Lake 2300 Parkway Blvd": 4.0,
            "Salt Lake City Ottinger Hall 233 Canyon Rd": 11.1,
            "Columbus Library 2530 S 500 E": 7.3,
            "Taylorsville City Hall 2600 Taylorsville Blvd": 1.0,
            "South Salt Lake Police 2835 Main St": 6.4,
            "Council Hall 300 State St": 11.1,
            "Redwood Park 3060 Lester St": 3.9,
            "Salt Lake County Mental Health 3148 S 1100 W": 4.3,
            "Salt Lake County/United Police Dept 3365 S 900 W": 4.4,
            "West Valley Prosecutor 3575 W Valley Central Sta bus Loop": 7.2,
            "Housing Auth. of Salt Lake County 3595 Main St": 5.3,
            "Utah DMV Administrative Office 380 W 2880 S": 6.0,
            "Third District Juvenile Court 410 S State St": 10.6,
            "Cottonwood Regional Softball Complex 4300 S 1300 E": 5.9,
            "Holiday City Office 4580 S 2300 E": 7.4,
            "Murray City Museum 5025 State St": 4.7,
            "Valley Regional Softball Complex 5100 South 2700 West": 0.6,
            "City Center of Rock Springs 5383 South 900 East #104": 6.4,
            "Rice Terrace Pavilion Park 600 E 900 South": 10.1,
            "Wheeler Historic Farm 6351 South 900 East": 10.1
        }
程序运行时出现的错误

File "C:/Users/hosea/C950HoseaGibson/Main/main.py", line 313, in <module>
    main()
  File "C:/Users/Main/main.py", line 310, in main
    dijkstrasShortestDistance(path, var1, visited)
  File "C:/Users/Main/main.py", line 217, in dijkstrasShortestDistance
    hasVisited = visited.get(finalItem)[0]
  File "C:/Users/Main/main.py", line 151, in get
    index = self.__calculateIndex(itemsKey)
  File "C:/Users/Main/main.py", line 181, in __calculateIndex
    index = hash(itemsKey) % len(self.__dataArray)
TypeError: unhashable type: 'list'
文件“C:/Users/hosea/C950HoseaGibson/Main/Main.py”,第313行,在
main()
文件“C:/Users/Main/Main.py”,第310行,在Main中
DijkstrassShortDistance(路径,var1,已访问)
文件“C:/Users/Main/Main.py”,第217行,dijkstrasShortestDistance
hasVisited=visited.get(finalItem)[0]
get中第151行的文件“C:/Users/Main/Main.py”
索引=自计算索引(itemsKey)
文件“C:/Users/Main/Main.py”,第181行,在计算索引中
索引=散列(itemsKey)%len(self.\uu数据数组)
TypeError:不可损坏的类型:“列表”

您发布了很多我懒得处理的代码,但它似乎试图使用列表(可能是
finalItem
)作为哈希表中的键(
chainingHashMap
),并且失败了,因为列表不可哈希。首先,再次检查以确保
finalItem
是它应该是的-手动或使用逐步调试程序遍历算法。如果它是它应该是的,你可以将它转换为tuple,它将成为可散列的-tuple是可散列的,list不是。@greenfoveguy我做了一个测试,所有的东西都通过了测试。