collections中的defaultdict

用类型

 用函数返回值 

嵌套的dict

from collections import defaultdict
def tree():
    return defaultdict(tree)


c = defaultdict(tree)
c['h']['username'] = 'xxx'
c['l']['username'] = 'xxxx'
c

  输出为:

defaultdict(<function __main__.tree>,
            {'h': defaultdict(<function __main__.tree>, {'username': 'xxx'}),
             'l': defaultdict(<function __main__.tree>, {'username': 'xxxx'})})

  

原文地址:https://www.cnblogs.com/bafenqingnian/p/9520500.html