• 首页
• 其他开发
• 关于Python 中出现的TypeError: missing 1 required positional argument

关于Python 中出现的TypeError: missing 1 required positional argument

问题描述

class Graph(object):

``````def __init__(self, *args, **kwargs):
self.node_neighbors = {}
self.visited = {}

for node in nodelist:

self.node_neighbors[node] = []

u, v = edge
if(v not in self.node_neighbors[u]) and (u not in self.node_neighbors[v]):
self.node_neighbors[u].append(u)
if(u!=v):
self.node_neighbors[v].append(u)

def nodes(self):
return self.node_neighbors.keys()

def depth_first_search(self, root=None):
order = []
def dfs(node):
self.visited[node] = True
order.append(node)
for  n in self.node_neighbors[node]:
if not n in self.visited:
dfs(n)
if root:
dfs(root)
for node in self.nodes():
if not node in self.visited:
dfs(node)
print(order)
return order

queue = []
order = []
def bfs():
while len(queue) >  0:
node = queue.pop()
self.visited[node] = True
for n in self.node_neighbors[node]:
if (not n in self.visited) and (not n in queue):
queue.append(n)
order.append(n)
if root:
queue.append(root)
order.append(root)
bfs()
for node in self.nodes():
if not node in self.visited:
queue.append(node)
order.append(node)
bfs()
print(order)
return order
``````

if name == '__main__':

``g = Graph()``

print('nodes:' + g.nodes())
order = g.depth_first_search(1)

``````def add_node(self,node):
self.node_neighbors[node] = []
``````

`if node not in self.add_nodes():`中的`add_nodes()`需要参数,具体要什么看你自己了.