簡體   English   中英

根據包含子字符串的字典從列表中的子字符串返回匹配值

[英]Return matching value from substring in a list against dictionary containing substring

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

[{'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 循環中迭代這兩個循環,看起來您需要在使用in檢查子字符串之前刪除switch值的最后兩個字符:

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

repl.it試試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM