1
私はダイクストラアルゴリズムを使用して最短パスファインダーをしようとしていたが、それは動作していないようです。問題が何であるか把握することはできません。コードとエラーメッセージは次のとおりです。 (。私は、Python 3.5に取り組んでいるhttps://www.youtube.com/watch?v=LHCVNtxb4ss)エラー 'AttributeError:' dict_keys 'オブジェクトに' remove '属性がありません。
graph = {
'A': {'B': 10, 'D': 4, 'F': 10},
'B': {'E': 5, 'J': 10, 'I': 17},
'C': {'A': 4, 'D': 10, 'E': 16},
'D': {'F': 12, 'G': 21},
'E': {'G': 4},
'F': {'E': 3},
'G': {'J': 3},
'H': {'G': 3, 'J': 3},
'I': {},
'J': {'I': 8},
}
def dijkstra(graph, start, end):
D = {}
P = {}
for node in graph.keys():
D[node]= -1
P[node]=""
D[start]=0
unseen_nodes=graph.keys()
while len(unseen_nodes) > 0:
shortest=None
node=' '
for temp_node in unseen_nodes:
if shortest==None:
shortest = D[temp_node]
node = temp_node
elif D[temp_node]<shortest:
shortest=D[temp_node]
node=temp_node
unseen_nodes.remove(node)
for child_node, child_value in graph[node].items():
if D[child_node] < D[node] + child_value:
D[child_node] = D[node] + child_value
P[child_node]=node
path = []
node = end
while not (node==start):
if path.count(node)==0:
path.insert(0, node)
node=P[node]
else:
break
path.insert(0, start)
return path
はAttributeError: 'dict_keys' オブジェクトは、Pythonの3ではremove
方法を持っていないdict.keys()
戻りa dict_keys object (a view of the dictionary)を