NoneType 对象不可迭代错误
NoneType object is not iterable Error
我的任务是为 Dijkstra 算法编写 classes。尽管不允许我编辑 Dijkstra class:
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
的 return 值吗?我该如何解决 w/o 编辑
class Djikstra()
阅读坦克。
为了作业
self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string)
为了工作,convert_to_adj_list
必须 return 一个包含三个值的元组被解压到这三个变量中。但是,您的方法 return 什么都没有(因此隐式地 returning None
)。将您的 convert_to_adj_list
方法更改为类似这样的方法,然后它应该可以工作:
def convert_to_adj_list(self, in_string):
... your code ...
return root, nnodes, adj_list
我的任务是为 Dijkstra 算法编写 classes。尽管不允许我编辑 Dijkstra class:
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
的 return 值吗?我该如何解决 w/o 编辑
class Djikstra()
阅读坦克。
为了作业
self.root, self.nnodes, self.adj_list = self.convert_to_adj_list(in_string)
为了工作,convert_to_adj_list
必须 return 一个包含三个值的元组被解压到这三个变量中。但是,您的方法 return 什么都没有(因此隐式地 returning None
)。将您的 convert_to_adj_list
方法更改为类似这样的方法,然后它应该可以工作:
def convert_to_adj_list(self, in_string):
... your code ...
return root, nnodes, adj_list