代码之家  ›  专栏  ›  技术社区  ›  Erika

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

  •  1
  • Erika  · 技术社区  · 3 年前

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

    {'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 我可以构建树,但无法创建完整路径。

    1 回复  |  直到 3 年前
        1
  •  1
  •   Ajax1234    3 年前

    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']
    
        2
  •  0
  •   kinshukdua    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")
    
        3
  •  0
  •   folen gateis    3 年前

    有差异的版本

    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)