Python -编辑和删除字典列表中的值



我有一个字典列表,我需要从interfaces键中删除任何东西,即Po......,我需要从Vlan接口中删除Vl,只是保持Vlan编号。我需要更改以下字典列表:

vrfs = [
{'default_rd': '<not set>',
'interfaces': ['Gi0/0'],
'name': 'Mgmt-vrf',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:510',
'interfaces': ['Po31.510', 'Po32.510', 'Vl503', 'Vl510', 'Vl515'],
'name': 'VLAN1',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:993',
'interfaces': ['Po31.993', 'Po32.993', 'Vl993'],
'name': 'VLAN2',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:855',
'interfaces': ['Po31.855', 'Po32.855', 'Vl855'],
'name': 'VLAN3',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:266',
'interfaces': ['Po31.266', 'Po32.266', 'Vl266'],
'name': 'VLAN4',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:248',
'interfaces': ['Po31.248', 'Po32.248', 'Vl248'],
'name': 'VLAN5',
'protocols': 'ipv4,ipv6'}
]

看起来像这样:

vrfs = [
{'default_rd': '<not set>',
'interfaces': ['Gi0/0'],
'name': 'Mgmt-vrf',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:510',
'interfaces': ['503', '510', '515'],
'name': 'VLAN1',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:993',
'interfaces': ['993'],
'name': 'VLAN2',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:855',
'interfaces': ['855'],
'name': 'VLAN3',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:266',
'interfaces': ['266'],
'name': 'VLAN4',
'protocols': 'ipv4,ipv6'},
{'default_rd': '12345:248',
'interfaces': ['248'],
'name': 'VLAN5',
'protocols': 'ipv4,ipv6'}
]

实现这一点的最好方法是什么?

尝试:

for d in vrfs:
d["interfaces"] = [v.replace("Vl", "") for v in d["interfaces"] if not v.startswith("Po")]
print(vrfs)

打印:

[
{
"default_rd": "<not set>",
"interfaces": ["Gi0/0"],
"name": "Mgmt-vrf",
"protocols": "ipv4,ipv6",
},
{
"default_rd": "12345:510",
"interfaces": ["503", "510", "515"],
"name": "VLAN1",
"protocols": "ipv4,ipv6",
},
{
"default_rd": "12345:993",
"interfaces": ["993"],
"name": "VLAN2",
"protocols": "ipv4,ipv6",
},
{
"default_rd": "12345:855",
"interfaces": ["855"],
"name": "VLAN3",
"protocols": "ipv4,ipv6",
},
{
"default_rd": "12345:266",
"interfaces": ["266"],
"name": "VLAN4",
"protocols": "ipv4,ipv6",
},
{
"default_rd": "12345:248",
"interfaces": ["248"],
"name": "VLAN5",
"protocols": "ipv4,ipv6",
},
]

似乎您只是试图更改列表中每个字典项中的'interfaces'键。下面的代码遍历列表中的每个字典项并修改接口键。

for dic in vrfs:
interfaces = dic.get('interfaces', None)
if interfaces:
# iterate through items in interfaces
res = []
for item in interfaces:
if item[:2] == "Po":
# ignore items that start with Po
continue
elif item[:2] == "Vl":
# ignore the 'Vl' part
res.append(item[2:])
else:
res.append(item)
dic['interfaces'] = res

我个人建议使用集合,因为它可以防止重复。

for d in vrfs:
temp_set=set()
for i in d["interfaces"]:
if(i[0:2]=="Po"):
temp_set.add(i[5:])
elif(i[0:2]=="Vl"):
temp_set.add(i[2:])
else:
temp_set.add(i)
d["interfaces"]=sorted(temp_set)
print(vrfs)

这在技术上是你想要的,但它需要(和任何代码一样)适应新的接口语法。

for vrf in vrfs:
interfaces = vrf['interfaces']
vrf['interfaces'] = []
for interface in interfaces:
if ('.' and 'Po') in interface: new_interface = interface.split('.')[1]
elif 'Vl' in interface: new_interface = interface[2:]
else: new_interface = interface
if new_interface not in vrf['interfaces']: vrf['interfaces'].append(new_interface)

相关内容

  • 没有找到相关文章

最新更新