Home > database >  Build full paths from list of nodes in python
Build full paths from list of nodes in python

Time:10-17

I have a dictionary that contains 2 lists.

{'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']}

I need to build the full path based on the lists in 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

What would be the best pythonic way to handle this? I have tried the binarytree and I could build the tree but could not manage to create the full paths.

CodePudding user response:

If you have a binarytree you can simply find path from root to that node.

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")

CodePudding user response:

a version with diff

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)

CodePudding user response:

A more generic approach with recursion and 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']))))

Output:

['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']
  • Related