1

I have a list of 'directory-tree-like' strings. I want to convert it into a nested list (or another kind of data structure), to emphasize the dependencies. for example, the input:

hierarchy_list

dir1
dir1/dir1.1                                            
dir1/dir1.2
dir1/dir1.3/dir1.3.1/dir1.3.1.1
dir1/dir1.1/dir1.1.1
dir1/dir1.1/dir1.1.2

need to be converted to:

dir1 ->
    dir1.1 -> dir1.1.1 , dir1.1.2
    dir1.2
    dir1.3 -> dir1.3.1 , dir1.3.2

where the '->' symbolize 'contains' (in a form of array attribute or equivalent).

dir1 contains list of dir1.1 , 1.2 , 1.3

dir1.1 contains list of dir1.1.1 , dir1.1.2

and so on...

Does someone have an idea how to do it in Python (algorithm and implementation)?

0

2 Answers 2

2

With a bit of recursion and a dict that can be done like:

Code:

def build_dir_dict(dir_name_strings):

    def _build_dir_dict(path_pieces, dir_dict):
        print(path_pieces)
        if path_pieces:
            if not path_pieces[0]:
                _build_dir_dict(path_pieces[1:], dir_dict)
            else:
                if path_pieces[0] not in dir_dict:
                    dir_dict[path_pieces[0]] = {}
                _build_dir_dict(path_pieces[1:], dir_dict[path_pieces[0]])

    result = {}
    for dir_name_string in dir_name_strings:
        if dir_name_string:
            _build_dir_dict(dir_name_string.split('/'), result)
    return result

Test Code:

data = [x.strip() for x in """
    dir1 
    dir1/dir1.1
    dir1/dir1.2 
    dir1/dir1.3/dir1.3.1/dir1.3.1.1 
    dir1/dir1.1/dir1.1.1 
    dir1/dir1.1/dir1.1.2
""".split('\n')[1:-1]]

print(build_dir_dict(data))

Results:

{'dir1': {'dir1.1': {'dir1.1.1': {}, 'dir1.1.2': {}}, 
          'dir1.2': {}, 
          'dir1.3': {'dir1.3.1': {'dir1.3.1.1': {}}}
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

THANK YOU! that looks really sweet solution. I had a thought about recursion, but it stayed in the back part of my head...
1

For each path, you can just split() it on the '/', then loop through them adding to an existing key or creating the key as needed and then assigning that to the current parent to be considered next. For example:

arr= ['dir1','dir1/dir1.1','dir1/dir1.2','dir1/dir1.3/dir1.3.1/dir1.3.1.1', 'dir1/dir1.1/dir1.1.1','dir1/dir1.1/dir1.1.2']

d = dict()
for path in arr:
    parent = d
    for dir in path.split('/'):    
        if dir not in parent:
            parent[dir] = dict()
        parent = parent[dir]

The result d will look like:

{'dir1': {'dir1.1': {'dir1.1.1': {}, 'dir1.1.2': {}},
          'dir1.2': {},
          'dir1.3': {'dir1.3.1': {'dir1.3.1.1': {}}}
          }
}

1 Comment

thanks, man! the thing is that I don't know how depth can be the hierarchy... look for the recursive solution above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.