Python中collections.defaultdict()使用

一个小示例

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"}}
posted @ 2018-02-07 16:21  囍。  阅读(1716)  评论(0编辑  收藏  举报