Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I split a string and form a multi-level nested dictionary?

I have a string like

foo/bar/baz

I also have val=1 for example. Is there a clean way to split the foo/bar/baz into a multi-dimensional dict with the last item in the dict to equal 1. So it would look like

{'foo': {'bar': {'baz': 1}}}
like image 829
Mike Avatar asked May 09 '15 03:05

Mike


2 Answers

You can use reduce and reversed functions, like this

>>> reduce(lambda res, cur: {cur: res}, reversed("foo/bar/baz".split("/")), 1)
{'foo': {'bar': {'baz': 1}}}

If you are using Python 3.x, then you need to import reduce from functools

>>> from functools import reduce
>>> reduce(lambda res, cur: {cur: res}, reversed("foo/bar/baz".split("/")), 1)
{'foo': {'bar': {'baz': 1}}}

Here, the last argument to reduce is the starting value. It will take values one by one from the iterable passed, call the function with the result and the current value and then the next time onwards, the last result will be the first argument and the current value as the second argument. When the iterable is exhausted, it will return the result.

So, the execution would have gone, step-by-step, as following

Let's say func is the lambda function and it gets repeatedly called like this

func(1, "baz")                   => {"baz": 1}
func({"baz": 1}, "bar")          => {"bar": {"baz": 1}}
func({"bar": {"baz": 1}}, "foo") => {"foo": {"bar": {"baz": 1}}}
like image 121
thefourtheye Avatar answered Oct 29 '22 09:10

thefourtheye


d = 1
for part in reversed(s.split('/')):
    d = {part: d}

If this needs to be extended to create something like a directory tree, you might want a solution based on defaultdict:

import collections

def tree():
    return collections.defaultdict(tree)

def parsetree(path_strings):
    t = tree()
    for s in path_strings:
        temp = t
        parts = s.split('/')
        for part in parts[:-1]:
            temp = temp[part]
        temp[parts[-1]] = 1
    return t

Demo:

>>> t = parsetree([
...     'foo/bar/baz',
...     'foo/bar/bop',
...     'foo/spam'
... ])
>>> t['foo']['bar']['baz']
1
>>> t['foo']['spam']
1
like image 34
user2357112 supports Monica Avatar answered Oct 29 '22 10:10

user2357112 supports Monica