Ach*_*nha 10 python recursion dictionary
我想实现以下目标:
foodict['foo.bar.baz'] = 'foo'
{
'foo': {
'bar': {
'baz': 'foo'
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
...递归创建密钥.
抓了一会儿之后,我想出了这个:
class Config(dict):
def __init__(self, *args, **kwargs):
self.super = super(Config, self)
self.update(*args, **kwargs)
def __setitem__(self, keys, value):
keys = keys.split('.')
keys.reverse()
config = Config()
for i, k in enumerate(keys):
if i == 0:
config = Config(**{ k: value })
else:
config = Config(**{ k: config })
self.super.update(config)
Run Code Online (Sandbox Code Playgroud)
您可以考虑Raymond Hettinger本人的"无限默认"食谱:
https://twitter.com/raymondh/status/343823801278140417
>>> from collections import defaultdict
>>> infinite_defaultdict = lambda: defaultdict(infinite_defaultdict)
>>> d = infinite_defaultdict()
>>> d['foo']['bar']['baz'] = 'foo'
>>> d
defaultdict(<function <lambda> at 0x1040388c8>, {'foo': defaultdict(<function <lambda> at 0x1040388c8>, {'bar': defaultdict(<function <lambda> at 0x1040388c8>, {'baz': 'foo'})})})
Run Code Online (Sandbox Code Playgroud)
另一种选择是实施__missing__:
>>> class InfiniteDict(dict):
... def __missing__(self, val):
... d = InfiniteDict()
... self[val] = d
... return d
...
>>> d = InfiniteDict()
>>> d['foo']['bar']['baz'] = 'foo'
>>> d
{'foo': {'bar': {'baz': 'foo'}}}
Run Code Online (Sandbox Code Playgroud)
如果您必须具有属性访问权限:
class InfiniteDict(dict):
def __missing__(self, val):
d = InfiniteDict()
self[val] = d
return d
def __getattr__(self, item):
return self.__getitem__(item)
def __setattr__(self, item, value):
super().__setitem__(item, value)
Run Code Online (Sandbox Code Playgroud)
在行动:
>>> d = InfiniteDict()
>>> d.foo.bar.baz = 'foo'
>>> d
{'foo': {'bar': {'baz': 'foo'}}}
>>>
Run Code Online (Sandbox Code Playgroud)
虽然,这有点快速和肮脏,所以不能保证没有任何错误.例如,与实际属性的碰撞几乎没有什么防范措施:
>>> d.keys = 'should I be allowed?'
>>> d
{'foo': {'bar': {'baz': 'foo'}}, 'keys': 'should I be allowed?'}
>>> d.keys()
dict_keys(['foo', 'keys'])
Run Code Online (Sandbox Code Playgroud)