from collections import defaultdict
def tree(): return defaultdict(tree)
#or tree = lambda: defaultdict(tree)
users = tree()
users['codingpy']['username'] = 'earlgrey'
users['python']['username'] = 'Guido van Rossum'
print(json.dumps(users))
){"codingpy": {"username": "earlgrey"}, "python": {"username": "Guido van Rossum"}}
categories = tree()
categories['Programming Languages']['Python']
categories['Python']['Standard Library']['sys']
categories['Python']['Standard Library']['os']
print(json.dumps(users))
){"Python": {"Standard Library": {"sys": {}, "os": {}}}, "Programming Languages": {"Python": {}}}
原文链接