python更新字典少写if-else的方法

dic = {'a':{'c':3, 'd':4}, 'b':{'f':5}}
# 不管你要往字典里添加什么,不需要写if-else,直接写以下代码
dic.setdefault('g',{})
dic['g'].update({'h':7})
print(dic)
# {'a': {'c': 3, 'd': 4}, 'b': {'f': 5}, 'g': {'h': 7}}
原文地址:https://www.cnblogs.com/leilijian/p/15017686.html