从python中的节点列表构建完整路径

2024-05-15 01:53:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本包含两个列表的字典

{'depth': [0, 1, 1, 2, 3, 3, 3, 1, 1, 2, 3], 'nodeName': ['root', 'Deleted Customers', 'New Customers', 'Region', 'Europe', 'Asia', 'America', 'Deleted Partners', 'New Partners', 'Region', 'Europe']}

我需要基于python中的列表构建完整路径

root\Deleted Customers
root\New Customers\Region\Europe
root\New Customers\Region\Asia
root\New Customers\Region\America
root\Deleted Partners
root\New Partners\Region\Europe

root
├── Deleted Customers
│
└── New Customers
   │
   └── Region
        |── Europe
        ├── Asia
        └── America

处理这个问题最好的方法是什么? 我尝试了binarytree,我可以构建树,但无法创建完整路径


Tags: 方法路径列表new字典rootregiondepth
3条回答

如果您有一个binarytree,您可以简单地找到从根节点到该节点的路径

def hasPath(root, arr, x):
     
    if (not root):
        return False
     
    arr.append(root.data)    

    if (root.data == x):    
        return True
     
    if (hasPath(root.left, arr, x) or
        hasPath(root.right, arr, x)):
        return True
  
    arr.pop(-1)
    return False
 
def printPath(root, x):
     
    # vector to store the path
    arr = []
    
    if (hasPath(root, arr, x)):
        for i in range(len(arr) - 1):
            print(arr[i], end = "/")
        print(arr[len(arr) - 1])
     
    else:
        print("No Path")

有差异的版本

current_path=['']*4
paths=[]
# extend the depth list for the diff
ext_depth=data['depth']+[data['depth'][-1]]
# diff will tell what paths you want to print
diff=list(map(lambda x: x[1]-x[0], zip(ext_depth[1:],ext_depth[:-1])))
for i, val in enumerate(data['depth']):
    # update path
    current_path[val]=data['nodeName'][i]
    if diff[i]>=0:
        # join only to the proper depth
        paths.append('\\'.join(current_path[:val+1]))
print(paths)

使用递归和itertools.groupby的更通用方法:

from itertools import groupby as gb
data = {'depth': [0, 1, 1, 2, 3, 3, 3, 1, 1, 2, 3], 'nodeName': ['root', 'Deleted Customers', 'New Customers', 'Region', 'Europe', 'Asia', 'America', 'Deleted Partners', 'New Partners', 'Region', 'Europe']}
def to_tree(n, vals):
    r = []
    for a, b in gb(vals, key=lambda x:x[0] == n):
       if a:
          r.extend([j for _, j in b])
       else:
          r = [*r[:-1], *[r[-1]+'\\'+j for j in to_tree(n+1, b)]]
    yield from r

paths = list(to_tree(0, list(zip(data['depth'], data['nodeName']))))

输出:

['root\\Deleted Customers', 
 'root\\New Customers\\Region\\Europe', 
 'root\\New Customers\\Region\\Asia', 
 'root\\New Customers\\Region\\America', 
 'root\\Deleted Partners', 
 'root\\New Partners\\Region\\Europe']

相关问题 更多 >

    热门问题