Home > other >  find a subset of base paths
find a subset of base paths

Time:10-10

I have a list of paths, for instance ['/usr/source', '/usr/source/linux', '/home/django', '/home/django/stuff', '/home/john'] and want to extract base paths from it, i.e. that would be: ['/usr/source', '/home/django', '/home/john'] but I struggle to find a way to do it.

CodePudding user response:

You can use a hashset or dictionary for storing bases:

Extract prefix from each path and put it into the hashset. After processing all paths, you'll have only base paths in the hashset and you can iterate them or convert them to an array.

Extracting prefix can be done different ways. One of them is this:

Split path to individual components (use slash as a separator) and then join first N (2 or 3) parts together again.

CodePudding user response:

This solves the given scenario but has limitations on '/home/john/profile/data' type scenario where it wouldn't count '/home/john' as base path and will exist as itself in the output list.

def base_path(text):
    words = text.split('/')
    x = len(text) - len(words[-1])
    return text[:x-1]


paths = ['/usr/source', '/usr/source/linux', '/home/django',
         '/home/django/stuff', '/home/john']
base_paths = []

for path in paths:
    if base_path(path) not in paths:
        base_paths.append(path)

print(base_paths)
  • Related