About TypeError: missing 1 required positional argument in Python
仅有的幸福
仅有的幸福 2017-06-14 10:51:00
0
1
2546

Started learning python and was looking at the part about data structures about graphs. This is to implement depth-first traversal and breadth-first traversal of a graph, but in the end, that is, g.add_nodes([i 1 for i in range(10)] ) will display TypeError: add_nodes() missing 1 required positional argument: 'nodelist' I don’t know how to solve it at all. Can anyone give me some advice?

This is my program

class Graph(object):

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

def add_nodes(self,nodelist):
    for node in nodelist:
        self.add_node(node)

def add_node(self,node):
    if node not in self.add_nodes():
        self.node_neighbors[node] = []

def add_edge(self,edge):
    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

def breadtg_frist_search(self, root = None):
    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()

g.add_nodes([i 1 for i in range(10)])
g.add_edge((1, 2))
g.add_edge((1, 3))
g. add_edge((2, 4))
g.add_edge((2, 5))
g.add_edge((4, 8))
g.add_edge((5, 8))
g.add_edge((5, 9))
g.add_edge((3, 6))
g.add_edge((3, 7))
g.add_edge((7, 10))
g.add_edge((9, 10))
print('nodes:' g.nodes())
order = g.breadtg_frist_search(1)
order = g.depth_first_search(1)

仅有的幸福
仅有的幸福

reply all(1)
学习ing
def add_node(self,node):
    if node not in self.add_nodes():
        self.node_neighbors[node] = []

if node not in self.add_nodes(): add_nodes() in requires parameters, it’s up to you what you need.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template