在python的嵌套字典中添加键 [英] adding keys in nested dictionary in python

查看:134
本文介绍了在python的嵌套字典中添加键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'}, 
            'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}

在氢和氦字典中添加一个is_noble_gas布尔条目.

Add an is_noble_gas boolean entry to the hydrogen and helium dictionaries.

推荐答案

您应该访问字典,并将is_noble_gas作为新关键字添加到字典中,如下所示.

You should access the dictionary and add is_noble_gas as a new key to the dictionary as follows.

>>> elements['hydrogen']['is_noble_gas'] = False
>>> elements['helium']['is_noble_gas'] = True

这篇关于在python的嵌套字典中添加键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆