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}}
python更新字典少写if-else的方法
转载文章标签 python基本写法 文章分类 Python 后端开发
本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
更新字典(Updating a Dictionary)
DescriptionIn this problem, a dictionary is collection of key-v
java UVa ACMICPC Updating a Dictionar 更新字典