如何在Python中更改嵌套字典中的键

2024-06-26 02:25:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的嵌套字典,我想让它更可读

 {'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”不存在

如何更改嵌套字典中的键


Tags: ofkeynoionumberdatatimevalue
2条回答

只需将dn1更改为以下内容:

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

注意:我已从dn1中的键中删除引号。
键的类型为dictionary1['IO Data']['n1']中的int,而您的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())

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

相关问题 更多 >