Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: recursively create dictionary from paths

I have several hundred thousand endpoint URLs that I want to generate stats for. For example I have:

/a/b/c
/a/b/d
/a/c/d
/b/c/d
/b/d/e
/a/b/c
/b/c/d

I want to create a dictionary that looks like this

{
    'a': {
        'b': {
            'c': {
                '_count': 2
            },
            'd': {
                '_count': 1
            }
        },
        'c': {
            'd': {
                '_count': 1
            }
        }
    },
    'b': {
        'c': {
            'd': {
                '_count': 2
            }
        },
        'd': {
            'e': {
                '_count': 1
            }
        }
    }
}

Any clever ways to do this?

EDIT

I should mention that the paths are not always 3 parts. There might be /a/b/c/d/e/f/g/h... etc, etc.

like image 522
sberry Avatar asked Jan 06 '10 23:01

sberry


1 Answers

Based on the answers, I wrote a general function for setting a dictionary value along a path:

def dictPath(path, dictionary, val, sep="/"):
    "set a value in a nested dictionary"
    while path.startswith(sep):
        path = path[1:]
    parts = path.split(sep, 1)
    if len(parts) > 1:
        branch = dictionary.setdefault(parts[0], {})
        dictPath(parts[1], branch, val, sep)
    else:
        dictionary[parts[0]] = val

like image 149
Ahmad Avatar answered Oct 14 '22 10:10

Ahmad