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