在python中获取最短路径时Dijkstra算法出错

2024-06-25 23:20:12 发布

您现在位置:Python中文网/ 问答频道 /正文

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

       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)

我使用的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'

Tags: incity参数mainfilesaltstpaths