collections中的defaultdict

时间:2023-03-08 18:29:41

用类型

collections中的defaultdict

用函数返回值

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