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}}}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…