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'})})

  

 

posted @ 2018-08-22 21:28  迷茫的计算机呆  阅读(212)  评论(0编辑  收藏  举报