AnswerBun.com

Return matching value from substring in a list against dictionary containing substring

Stack Overflow Asked by jajao555 on December 11, 2020

I have a list of dictionaries like below.

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

I also have a list containing values like below.

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

I’d like be able to extract a substring from the {‘switch’:’015ABC003999FR’} value and have the value 000015ABC003999CIRC returned and appended to that same dictionary as {‘circ’:’000015ABC003999CIRC’}.

I haven’t been able to find an example close to that scenario. Is it possible to do like a regex for 015ABC003999 of the dictionary then also match on a regex of the list? I think another option would be to just create the value adding zeroes to the front and replacing the FR with CIRC but I’d rather match to the list as a sort of verification.

I’d then use the dictionary to populate configurations sort of like below

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

One Answer

You can just iterate over both in two for loops, it looks like you need to remove the last two characters of the switch values before checking for a substring using in:

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')

Output:

[
  {
    "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

Try it on repl.it

Correct answer by Shash Sinha on December 11, 2020

Add your own answers!

Related Questions

Update a with react

1  Asked on December 30, 2021 by mara-oliveira

     

Euclidean distance of all pandas rows to single row

1  Asked on December 30, 2021 by aquamad96

     

Different result with online compiler and visual code

0  Asked on December 30, 2021 by swayamjeet-swain

   

recursive function debug asserion failed

2  Asked on December 30, 2021 by steven-zhou

   

SQL Grant Execute On Object where do I put GO?

2  Asked on December 30, 2021 by andy-williams

   

Split string by comma unless followed by a space or a ‘+’

3  Asked on December 30, 2021 by callum-brown

       

Get values of cells in dataframe column quickly

2  Asked on December 30, 2021 by user11035198

     

Parsing XML values in Javascript

2  Asked on December 30, 2021 by topcat3

   

split sequences

1  Asked on December 30, 2021 by backgroup

 

when checkbox checked value true, display text not displayed

4  Asked on December 30, 2021 by user11247496

     

.net core 2.0 logging inside Kubernetes pod console

1  Asked on December 30, 2021 by abhay

       

XML vs JSON vs SQLite for only reading data

9  Asked on December 30, 2021 by brandon-cornelio

   

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP