Python 根据包含子字符串的字典从列表中的子字符串返回匹配值

Python 根据包含子字符串的字典从列表中的子字符串返回匹配值,python,Python,我有一个字典列表,如下所示 [{'cvid': '642', 'switch': '015ABC003999FR', 'uni': '5'}, {'cvid': '523', 'switch': '017ABC001230FR', 'uni': '5'}, {'cvid': '43', 'switch': '017ABC001231FR', 'uni': '2'}, {'cvid': '45', 'switch': '500ABC005437FR', 'uni': '3'}] [['00

我有一个字典列表,如下所示

[{'cvid': '642', 'switch': '015ABC003999FR', 'uni': '5'},
 {'cvid': '523', 'switch': '017ABC001230FR', 'uni': '5'},
 {'cvid': '43', 'switch': '017ABC001231FR', 'uni': '2'},
 {'cvid': '45', 'switch': '500ABC005437FR', 'uni': '3'}]
[['000015ABC003999CIRC'], ['000017ABC001230CIRC'], ['000017ABC001231CIRC'], ['000015ABC000249CIRC'], ['000500ABC005437CIRC']]
我还有一个包含如下值的列表

[{'cvid': '642', 'switch': '015ABC003999FR', 'uni': '5'},
 {'cvid': '523', 'switch': '017ABC001230FR', 'uni': '5'},
 {'cvid': '43', 'switch': '017ABC001231FR', 'uni': '2'},
 {'cvid': '45', 'switch': '500ABC005437FR', 'uni': '3'}]
[['000015ABC003999CIRC'], ['000017ABC001230CIRC'], ['000017ABC001231CIRC'], ['000015ABC000249CIRC'], ['000500ABC005437CIRC']]
我希望能够从{'switch':'015ABC003999FR'}值中提取一个子字符串,并将值000015ABC003999CIRC返回并附加到与{'circ':'000015ABC003999CIRC'相同的字典中

我还没有找到一个接近这种情况的例子。是否有可能像字典的015ABC003999一样使用正则表达式,然后在列表的正则表达式上进行匹配?我认为另一种选择是只在前面创建一个加零的值,并用CIRC替换FR,但我更愿意匹配列表作为一种验证

然后我会使用字典来填充配置,如下所示

print('cfm service delete service ' + dic['switch'])
print('cfm mep create service ' + dic['circ'] + ' port ' +  dic['uni'] + ' type up vlan ' +  dic['cvid'] + ' mepid 3')

您可以在两个for循环中迭代这两个值,在使用
中的
检查子字符串之前,似乎需要删除
开关
值的最后两个字符:

import json

data = [
          {
            "cvid": "642",
            "switch": "015ABC003999FR",
            "uni": "5"
          },
          {
            "cvid": "523",
            "switch": "017ABC001230FR",
            "uni": "5"
          },
          {
            "cvid": "43",
            "switch": "017ABC001231FR",
            "uni": "2"
          },
          {
            "cvid": "45",
            "switch": "500ABC005437FR",
            "uni": "3"
          }
        ]

lst = [['000015ABC003999CIRC'], ['000017ABC001230CIRC'], ['000017ABC001231CIRC'],
      ['000015ABC000249CIRC'], ['000500ABC005437CIRC']]

for d in data:
  switch_val_without_last_2_ch = d["switch"][:-2]
  for sub_lst in lst:
    val = sub_lst[0]
    if switch_val_without_last_2_ch in val:
      d["circ"] = val
      break

print(json.dumps(data, indent=2, sort_keys=False))

for dic in data:
  print(f'cfm service delete service {dic["switch"]}')
  print(f'cfm mep create service {dic["circ"]} port {dic["uni"]} type up vlan {dic["cvid"]} mepid 3')
输出:

[
  {
    "cvid": "642",
    "switch": "015ABC003999FR",
    "uni": "5",
    "circ": "000015ABC003999CIRC"
  },
  {
    "cvid": "523",
    "switch": "017ABC001230FR",
    "uni": "5",
    "circ": "000017ABC001230CIRC"
  },
  {
    "cvid": "43",
    "switch": "017ABC001231FR",
    "uni": "2",
    "circ": "000017ABC001231CIRC"
  },
  {
    "cvid": "45",
    "switch": "500ABC005437FR",
    "uni": "3",
    "circ": "000500ABC005437CIRC"
  }
]
cfm service delete service 015ABC003999FR
cfm mep create service 000015ABC003999CIRC port 5 type up vlan 642 mepid 3
cfm service delete service 017ABC001230FR
cfm mep create service 000017ABC001230CIRC port 5 type up vlan 523 mepid 3
cfm service delete service 017ABC001231FR
cfm mep create service 000017ABC001231CIRC port 2 type up vlan 43 mepid 3
cfm service delete service 500ABC005437FR
cfm mep create service 000500ABC005437CIRC port 3 type up vlan 45 mepid 3

上尝试一下“但我宁愿与列表匹配作为一种验证”是什么意思?预期的输出是什么?以防我创建字典的设备有额外的电路。我想这张单子有点像一个知名的好东西。还增加了最终目标。