我有一个嵌套的dict,键作为整数,我试图使用每个元素值中的名称作为字典的键,但是我犯了一些错误。有人能告诉我,我的代码出了什么问题,实现我的目标的最好的节奏曲方法是什么?提前谢谢。
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 = dict.items()中(键、值)的值
listOfKeys =[dict.items()中(key,value)的键‘’name‘]
dict2 = zip(listOfKeys,listOfValues)
错误:
listOfKeys = [key['name'] for (key, value) in dict.items()]
TypeError: 'int' object is not subscriptable发布于 2019-09-11 09:05:39
dict = {v['name']: v for v in dict.values()}尝尝这个
发布于 2019-09-11 08:50:51
别干那事!您应该列出一个字典列表,而不是以整数作为键:
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。
dictList[0] 发布于 2019-09-11 09:10:57
首先:不要使用dict作为变量,因为dict是一个函数
,所以请使用dict1来代替
然后:dict的密钥是int服从
试试这个:
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': ''}}}https://stackoverflow.com/questions/57885383
复制相似问题