Dijkstra의 알고리즘에 대한 클래스를 작성하는 작업이 있습니다. Dijkstra 클래스를 편집 할 수는 없지만 :
class Dijkstra():
# initialize with a string containing the root and a
# weighted edge list
def __init__(self, in_string):
self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string)
self.nodes = [Node(i) for i in range(self.nnodes)]
self.nodes[self.root].key = 0
self.heap = MinHeap(self.nodes)
# the input is expected to be a string
# consisting of the number of nodes
# and a root followed by
# vertex pairs with a non-negative weight
def convert_to_adj_list(self, in_string):
nnodes, root, edges = in_string.split(';')
root = int(root)
nnodes = int(nnodes)
adj_list = {}
edges = [ map(int,wedge.split()) for wedge in edges.split(',')]
for u,v,w in edges:
(adj_list.setdefault(u,[])).append((v,w))
for u in range(nnodes):
adj_list.setdefault(u,[])
이것은 내 문제입니다.
string = '3; 0; 1 2 8, 2 0 5, 1 0 8, 2 1 3'
print(Dijkstra(string))
Traceback (most recent call last):
File "<pyshell#321>", line 1, in <module>
print(Dijkstra(string))
File "C:\Users\TheDude\Downloads\dijkstra.py", line 71, in __init__
self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string)
TypeError: 'NoneType' object is not iterable
append
의 반환 값을 할당 합니까? class Djikstra()
읽기를 위해 탱크를 편집하지 않고 어떻게 고칠 수 있습니까 ?
과제를 위해
self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string)
작동 convert_to_adj_list
하려면 세 가지 변수로 풀릴 세 값의 튜플을 반환해야합니다. 그러나 귀하의 메서드는 아무것도 반환하지 않으므로 암시 적으로 반환 None
합니다. convert_to_adj_list
방법을 다음과 같이 변경 하면 작동합니다.
def convert_to_adj_list(self, in_string):
... your code ...
return root, nnodes, adj_list
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다