如何使用西雅图环境在repy中导入lib

2024-06-09 18:24:20 发布

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

我必须在repy中导入一个lib到我的项目中,但是它返回了一个犯规。 我想用Dijkstra算法模块,在西雅图的船舶上运行,以显示船舶之间的最短路径。在

from priodict import priorityDictionary

def Dijkstra(G,start,end=None):

 D = {} # dictionary of final distances
 P = {} # dictionary of predecessors
 Q = priorityDictionary()   # est.dist. of non-final vert.
 Q[start] = 0

 for v in Q:
    D[v] = Q[v]
    if v == end: break

    for w in G[v]:
        vwLength = D[v] + G[v][w]
        if w in D:
            if vwLength < D[w]:
                raise ValueError, \
  "Dijkstra: found better path to already-final vertex"
        elif w not in Q or vwLength < Q[w]:
            Q[w] = vwLength
            P[w] = v

return (D,P)

def shortestPath(G,start,end):

D,P = Dijkstra(G,start,end)
Path = []
while 1:
    Path.append(end)
    if end == start: break
    end = P[end]
Path.reverse()
return Path

它会抛出这样的错误:

^{pr2}$

我该怎么做才能让它生效?在


Tags: ofpathinfordictionaryifdefstart