我有一個字典串列,我需要從interfaces鍵中洗掉任何內容,Po......并且我需要Vl從 Vlan 介面中洗掉,只保留 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'}
]
實作這一目標的最佳方法是什么?
uj5u.com熱心網友回復:
嘗試:
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",
},
]
uj5u.com熱心網友回復:
您似乎只是想更改串列中每個字典項中的“介面”鍵。下面是一個代碼,它遍歷串列中的每個字典項并修改介面鍵。
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
uj5u.com熱心網友回復:
我個人會建議使用一個設定,因為它可以防止重復。
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)
uj5u.com熱心網友回復:
這在技術上可以按您的意愿作業,但它需要(與任何代碼一樣)適應新的介面語法。
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)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/471962.html
上一篇:根據嵌套值對字典串列進行排序
下一篇:使用一列值為可變序號列創建計數器
