adding keys in nested dictionary in python

前端 未结 3 1504
温柔的废话
温柔的废话 2021-01-23 02:04
elements = {\'hydrogen\': {\'number\': 1, \'weight\': 1.00794, \'symbol\': \'H\'}, 
            \'helium\': {\'number\': 2, \'weight\': 4.002602, \'symbol\': \'He\'}}
         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-23 02:19

    elements = {'hydrogen': {'number': 1, 'weight': 1.00794, 'symbol': 'H'}, 'helium': {'number': 2, 'weight': 4.002602, 'symbol': 'He'}}
    noble_dict = { "hydrogen": False, "helium": True }
    for noble in noble_dict:
        elements[ noble ][ "is_noble_gas" ] = noble_dict[ noble ]
    

    {'helium': {'symbol': 'He', 'number': 2, 'weight': 4.002602, 'is_noble_gas': True}, 'hydrogen': {'symbol': 'H', 'number': 1, 'weight': 1.00794, 'is_noble_gas': False}}

提交回复
热议问题