Python中collections.defaultdict()使用

时间:2021-03-02 14:52:02

一个小示例

from collections import  defaultdict
import json def tree():
return defaultdict(tree) users = tree()
users['harold']['username'] = 'hrldcpr'
users['handler']['username'] = 'matthandlersux' print(json.dumps(users))

输出:

{"harold": {"username": "hrldcpr"}, "handler": {"username": "matthandlersux"}}