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')
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
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP