如何找到从给定节点到 python 中所有叶节点的路径
How to find path from a given node to all leaf nodes in python
我有一个字典,其中包含与每个节点关联的父节点和子节点列表(代码中的字典引用)。我将输入一个键(对于以下代码 B
是键)。我必须考虑父节点。对于 B
我想要的路径为 [B,C,C1,X],[B,C,C2],[B,D,D1],[B,D,D2]。
我获得的以下代码的输出是:
C ['C1', 'C2']
C1 ['X']
我也收到以下错误:
File "", line 7, in path_find
if d[i]['parent'] == [key]:
KeyError: 'X'
def path_find(graph,key):
x = d[key]['child']
for i in x:
if d[i]['parent'] == [key]:
print(i,d[i]['child'])
path_find(d,i)
d = {'B':{
'parent' : ['A'],
'child': ['C','D']},
'C':{
'parent' : ['B'],
'child' : ['C1','C2']},
'D':{
'parent' : ['B'],
'child': ['D1','D2']},
'C1':{
'parent' : ['C'],
'child': ['X']}}
key = 'B'
path_find(d,key)
预期输出为:[B, C, C1, X], [B, C, C2], [B, D, D1], [B, D, D2]
实际输出是:
C ['C1', 'C2']
C1 ['X']
您的代码中有一些错误:
1) 您没有在 d = { ... }
中添加关于 X
节点的信息,这就是为什么您得到了 KeyError
。我想这是一个没有 children.
的节点
2) 您没有保存当前节点的路径,因此您的输出无效。
更正后的代码(加上我的评论):
def path_find(graph, key, current_path, paths_list): # graph, current node key, path to current node, all paths list
if key not in d: # not in graph - no children
paths_list.append(current_path)
return
children = d[key]['child']
for child in children: # check all children
path_find(graph, child, current_path[:] + [child], paths_list)
if not children: # no children - finish search
paths_list.append(current_path)
d = {'B':{
'parent' : ['A'],
'child': ['C','D']},
'C':{
'parent' : ['B'],
'child' : ['C1','C2']},
'D':{
'parent' : ['B'],
'child': ['D1','D2']},
'C1':{
'parent' : ['C'],
'child': ['X']}}
key = 'B'
paths_list = []
path_find(d, key, [key], paths_list)
print(paths_list)
输出:
[['B', 'C', 'C1', 'X'], ['B', 'C', 'C2'], ['B', 'D', 'D1'], ['B', 'D', 'D2']]
我有一个字典,其中包含与每个节点关联的父节点和子节点列表(代码中的字典引用)。我将输入一个键(对于以下代码 B
是键)。我必须考虑父节点。对于 B
我想要的路径为 [B,C,C1,X],[B,C,C2],[B,D,D1],[B,D,D2]。
我获得的以下代码的输出是:
C ['C1', 'C2']
C1 ['X']
我也收到以下错误:
File "", line 7, in path_find if d[i]['parent'] == [key]:
KeyError: 'X'
def path_find(graph,key):
x = d[key]['child']
for i in x:
if d[i]['parent'] == [key]:
print(i,d[i]['child'])
path_find(d,i)
d = {'B':{
'parent' : ['A'],
'child': ['C','D']},
'C':{
'parent' : ['B'],
'child' : ['C1','C2']},
'D':{
'parent' : ['B'],
'child': ['D1','D2']},
'C1':{
'parent' : ['C'],
'child': ['X']}}
key = 'B'
path_find(d,key)
预期输出为:[B, C, C1, X], [B, C, C2], [B, D, D1], [B, D, D2]
实际输出是:
C ['C1', 'C2']
C1 ['X']
您的代码中有一些错误:
1) 您没有在 d = { ... }
中添加关于 X
节点的信息,这就是为什么您得到了 KeyError
。我想这是一个没有 children.
2) 您没有保存当前节点的路径,因此您的输出无效。
更正后的代码(加上我的评论):
def path_find(graph, key, current_path, paths_list): # graph, current node key, path to current node, all paths list
if key not in d: # not in graph - no children
paths_list.append(current_path)
return
children = d[key]['child']
for child in children: # check all children
path_find(graph, child, current_path[:] + [child], paths_list)
if not children: # no children - finish search
paths_list.append(current_path)
d = {'B':{
'parent' : ['A'],
'child': ['C','D']},
'C':{
'parent' : ['B'],
'child' : ['C1','C2']},
'D':{
'parent' : ['B'],
'child': ['D1','D2']},
'C1':{
'parent' : ['C'],
'child': ['X']}}
key = 'B'
paths_list = []
path_find(d, key, [key], paths_list)
print(paths_list)
输出:
[['B', 'C', 'C1', 'X'], ['B', 'C', 'C2'], ['B', 'D', 'D1'], ['B', 'D', 'D2']]