简体   繁体   English

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

[英]How can I change keys in a nested dictionary in Python

I have the following nested dictionary and I want to make it more readable.我有以下嵌套字典,我想让它更具可读性。

 {'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'}

In my script I did the following:在我的脚本中,我做了以下事情:

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)

And the result:结果:

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'}

I want to do the same thing for 'IO Data' ('n1', 'n2', etc).我想对“IO 数据”(“n1”、“n2”等)做同样的事情。 I tried to do:我试着做:

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)

But I get an error saying '239'.但是我收到一条错误消息“239”。 That meant the '239' did not exist.这意味着“239”不存在。

How can I change the keys in a nested dictionary?如何更改嵌套字典中的键?

Just change your dn1 to below:只需将您的dn1更改为以下内容:

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

Note: I have removed quotes from the keys in dn1 .注意:我已从dn1中的键中删除引号。
The keys are of type int in dictionary1['IO Data']['n1'] , while your dn1 has mapping as str键的类型为int in dictionary1['IO Data']['n1'] ,而您的dn1具有映射为str

Here is the entire code:这是完整的代码:

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)

Or if you want to use the same dn1 you have provided in the question then use the below code:或者,如果您想使用您在问题中提供的相同dn1 ,请使用以下代码:

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

The keys in dn1 are of type string while the keys in dictionary1['IO Data']['n1'].items() are of type int. dn1中的键是字符串类型,而dictionary1['IO Data']['n1'].items()中的键是 int 类型。 You need the types to match.您需要类型匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM