【问题标题】:How can I change keys in a nested dictionary in Python如何在 Python 中更改嵌套字典中的键
【发布时间】:2021-08-26 13:43:27
【问题描述】:

我有以下嵌套字典,我想让它更具可读性。

 {'sys_time': '26/08/2021 13:08:19', 'codecid': 8, 'no_record_i': 1, 'no_record_e': 1, 'crc-16': 47289, 'd_time_unix': 1629979644000, 'd_time_local': '2021-08-26 13:07:24', 'priority': 0, 'lon': 0, 'lat': 0, 'alt': 0, 'angle': 0, 'satellites': 0, 'speed': 0, 'io_data': {'n1': {239: 1, 240: 1, 80: 1, 21: 4, 200: 0, 69: 2}, 'n2': {181: 0, 182: 0, 66: 14122, 24: 0, 67: 0, 68: 0}, 'n4': {241: 26806, 16: 0}}, 'imei': '359633104643825'}

在我的脚本中,我执行了以下操作:

d1 = {'sys_time': 'System time', 'codecid': 'Codec ID', 'no_record_i': 'Number of records i', 'no_record_e': 'Number of records e', 'crc-16': 'CRC-16', 'd_time_unix': 'Time Unix', 'd_time_local': 'Time Local', 'priority': 'Priority', 'lon': 'Longitude', 'lat': 'Latitude', 'alt': 'Altitude', 'angle': 'Angle', 'satellites': 'Satellites', 'speed': 'Speed', 'io_data': 'IO Data', 'imei': 'IMEI'}
                
dictionary1 = dict((d1[key], value) for (key, value) in vars.items())
               
print("dictionary1", dictionary1)

结果:

dictionary1 {'System time': '26/08/2021 12:55:52', 'Codec ID': 8, 'Number of records i': 7, 'Number of records e': 7, 'CRC-16': 8664, 'Time Unix': 1629978933000, 'Time Local': '2021-08-26 12:55:33', 'Priority': 0, 'Longitude': 0, 'Latitude': 0, 'Altitude': 0, 'Angle': 0, 'Satellites': 0, 'Speed': 0, 'IO Data': {'n1': {239: 1, 240: 1, 80: 1, 21: 4, 200: 0, 69: 2}, 'n2': {181: 0, 182: 0, 66: 14132, 24: 0, 67: 0, 68: 0}, 'n4': {241: 26806, 16: 0}}, 'IMEI': '359633104643825'}

我想对“IO 数据”(“n1”、“n2”等)做同样的事情。 我试着做:

dn1 = {'239': 'Ignition', '240': 'Movement', '80': 'Data Mode', '21': 'GSM Signal',
                       '200': 'Sleep Mode', '69': 'GNSS Status'}

dictionary2 = dict((dn1[key], value) for (key, value) in dictionary1['IO Data']['n1'].items())
print("dictionary2", dictionary2)

但我收到一条错误消息“239”。这意味着“239”不存在。

如何更改嵌套字典中的键?

【问题讨论】:

    标签: python python-3.x dictionary nested


    【解决方案1】:

    只需将您的dn1 更改为以下:

    dn1 = {239: 'Ignition', 240: 'Movement', 80: 'Data Mode', 21: 'GSM Signal',
                           200: 'Sleep Mode', 69: 'GNSS Status'}
    

    注意:我已从 dn1 中的键中删除引号。
    键的类型为 int in dictionary1['IO Data']['n1'] ,而您的 dn1 映射为 str

    完整代码如下:

    vars = {'sys_time': '26/08/2021 13:08:19', 'codecid': 8, 'no_record_i': 1, 'no_record_e': 1, 'crc-16': 47289, 'd_time_unix': 1629979644000, 'd_time_local': '2021-08-26 13:07:24', 'priority': 0, 'lon': 0, 'lat': 0, 'alt': 0, 'angle': 0, 'satellites': 0, 'speed': 0, 'io_data': {'n1': {239: 1, 240: 1, 80: 1, 21: 4, 200: 0, 69: 2}, 'n2': {181: 0, 182: 0, 66: 14122, 24: 0, 67: 0, 68: 0}, 'n4': {241: 26806, 16: 0}}, 'imei': '359633104643825'}
    
    d1 = {'sys_time': 'System time', 'codecid': 'Codec ID', 'no_record_i': 'Number of records i', 'no_record_e': 'Number of records e', 'crc-16': 'CRC-16', 'd_time_unix': 'Time Unix', 'd_time_local': 'Time Local', 'priority': 'Priority', 'lon': 'Longitude', 'lat': 'Latitude', 'alt': 'Altitude', 'angle': 'Angle', 'satellites': 'Satellites', 'speed': 'Speed', 'io_data': 'IO Data', 'imei': 'IMEI'}
                    
    dictionary1 = dict((d1[key], value) for (key, value) in vars.items())
                   
    print("dictionary1", dictionary1)
    dn1 = {239: 'Ignition', 240: 'Movement', 80: 'Data Mode', 21: 'GSM Signal',
                           200: 'Sleep Mode', 69: 'GNSS Status'}
    
    dictionary2 = dict((dn1[key], value) for (key, value) in dictionary1['IO Data']['n1'].items())
    print("dictionary2", dictionary2)
    

    或者,如果您想使用问题中提供的 dn1,请使用以下代码:

    dictionary2 = dict((dn1[str(key)], value) for (key, value) in dictionary1['IO Data']['n1'].items())
    

    【讨论】:

    • 非常感谢!
    【解决方案2】:

    dn1 中的键是 string 类型,而 dictionary1['IO Data']['n1'].items() 中的键是 int 类型。您需要匹配的类型。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-01
      • 2021-07-29
      • 2021-04-14
      • 2018-07-10
      • 2011-07-02
      • 1970-01-01
      • 2019-07-18
      • 2019-07-15
      相关资源
      最近更新 更多