Python 3.x 如何从对应的键值更改嵌套的dict键
我有一个键为整数的嵌套dict,我试图使用每个元素值中的名称作为字典的键,但我有一些错误。 有人能告诉我我的代码有什么问题吗?实现我的目标最好的pythonic方法是什么?提前谢谢Python 3.x 如何从对应的键值更改嵌套的dict键,python-3.x,Python 3.x,我有一个键为整数的嵌套dict,我试图使用每个元素值中的名称作为字典的键,但我有一些错误。 有人能告诉我我的代码有什么问题吗?实现我的目标最好的pythonic方法是什么?提前谢谢 dict= { 1: { 'name': 'PLS_1-2-3-4-5-6_IPVPN_101_1', 'qos': 'ef', 'police': { 'cir': '100', 'cbs': '6400',
dict=
{
1: {
'name': 'PLS_1-2-3-4-5-6_IPVPN_101_1',
'qos': 'ef',
'police': {
'cir': '100',
'cbs': '6400',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
}
},
2: {
'name': 'policer_RT_257',
'qos': 'cs7',
'police': {
'cir': '10000000',
'cbs': '16384',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
}
},
3: {
'name': 'PW_VPN_Test_2_PW',
'qos': 'ef',
'police': {
'cir': '10000',
'cbs': '640000',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
}
}
}
预期的
dict2={
'PLS_1-2-3-4-5-6_IPVPN_101_1': {
'name': 'PLS_1-2-3-4-5-6_IPVPN_101_1',
'qos': 'ef',
'police': {
'cir': '100',
'cbs': '6400',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
}
},
'policer_RT_257': {
'name': 'policer_RT_257',
'qos': 'cs7',
'police': {
'cir': '10000000',
'cbs': '16384',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
}
},
'PW_VPN_Test_2_PW': {
'name': 'Tef_PW_VPN_Test_2_PW',
'qos': 'ef',
'police': {
'cir': '10000',
'cbs': '640000',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
}
}
}
我曾尝试使用key、value迭代作为键和值的新列表,然后将它们压缩为新字典,但出现了一些错误
```
listOfValues = [value for (key, value) in dict.items()]
listOfKeys = [key['name'] for (key, value) in dict.items()]
dict2 = zip(listOfKeys, listOfValues)
```
错误:
listOfKeys = [key['name'] for (key, value) in dict.items()]
TypeError: 'int' object is not subscriptable
不要那样做!你应该列出字典而不是整数 作为密钥:
dictList = [
{
'name': 'PLS_1-2-3-4-5-6_IPVPN_101_1',
'qos': 'ef',
'police': {
'cir': '100',
'cbs': '6400',
},
'marker': {
'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''
},
...
]
现在,您可以通过索引访问dictList
:
首先试试这个:不要把
dict
作为变量,因为dict
是一个函数
,所以改用dict1作为例子
然后:dict
的键是int-object
试试这个:
listOfValues = [value for (key, value) in dict1.items()]
listOfKeys = [dict1[key]['name'] for (key, value) in dict1.items()]
dict2 = dict(zip(listOfKeys, listOfValues))
dict2
输出:
{'PLS_1-2-3-4-5-6_IPVPN_101_1': {'name': 'PLS_1-2-3-4-5-6_IPVPN_101_1',
'qos': 'ef',
'police': {'cir': '100', 'cbs': '6400'},
'marker': {'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''}},
'policer_RT_257': {'name': 'policer_RT_257',
'qos': 'cs7',
'police': {'cir': '10000000', 'cbs': '16384'},
'marker': {'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''}},
'PW_VPN_Test_2_PW': {'name': 'PW_VPN_Test_2_PW',
'qos': 'ef',
'police': {'cir': '10000', 'cbs': '640000'},
'marker': {'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''}}}
大家好,欢迎来到StackOverflow。请花些时间阅读帮助页面,特别是命名和的部分。更重要的是,请阅读。您可能还想了解。
listOfValues = [value for (key, value) in dict1.items()]
listOfKeys = [dict1[key]['name'] for (key, value) in dict1.items()]
dict2 = dict(zip(listOfKeys, listOfValues))
dict2
{'PLS_1-2-3-4-5-6_IPVPN_101_1': {'name': 'PLS_1-2-3-4-5-6_IPVPN_101_1',
'qos': 'ef',
'police': {'cir': '100', 'cbs': '6400'},
'marker': {'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''}},
'policer_RT_257': {'name': 'policer_RT_257',
'qos': 'cs7',
'police': {'cir': '10000000', 'cbs': '16384'},
'marker': {'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''}},
'PW_VPN_Test_2_PW': {'name': 'PW_VPN_Test_2_PW',
'qos': 'ef',
'police': {'cir': '10000', 'cbs': '640000'},
'marker': {'use-pre-set-markings': 'false',
'cir-conform-action': 'none',
'cir-exceed-action': 'drop',
'pir-exceed-action': ''}}}