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

Python - Edit and remove values in a list of dictionaries

我有一个字典列表,我需要从 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'}
]

完成此任务的最佳方法是什么?

您似乎只是想更改列表中每个字典项中的 '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 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",
    },
]

这在技术上可以如您所愿地工作,但它需要(与任何代码一样)适应新的接口语法。

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)