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.
Asked
Active
Viewed 40 times
-1
-
Why is the result not `['/usr', '/home']`? – mkrieger1 Oct 10 '22 at 14:05
-
Welcome to [Stack Overflow.](https://stackoverflow.com/ "Stack Overflow") This is not a code-writing or tutoring service. We help solve specific, technical problems, not open-ended requests for code or advice. Please edit your question to show what you have tried so far, and what specific problem you need help with. See the [How To Ask a Good Question](https://stackoverflow.com/help/how-to-ask "How To Ask a Good Question") page for details on how to best help us help you. – itprorh66 Oct 10 '22 at 14:08
-
Those are not members of the list. – erg Oct 10 '22 at 14:09
-
Does this answer your question? [How do I get the parent directory in Python?](https://stackoverflow.com/questions/2860153/how-do-i-get-the-parent-directory-in-python) – DarrylG Oct 10 '22 at 14:11
2 Answers
0
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.

Jiri Volejnik
- 1,034
- 6
- 9
0
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)

Asif
- 38
- 3