Reputation: 229
I am new to python, I have nested list where i want to create dictionary like below using this data.
This is my input:
input =
[['103.29.45.128/27', '32', 'ge', 'MPLS-LOOPBACK'],
['100.127.0.0/23', '32', 'ge', 'MPLS-LOOPBACK'],
['100.92.57.168/30', '', '', 'PS_S1']
]
The Expected output should be like below:
result = "prefix_set": [
{
"prefix_entry": [
{
"prefix": "103.29.45.128/27",
"prefix_match": "32",
"prefix_condition": "ge"
},
{
"prefix": "100.127.0.0/23",
"prefix_match": "32",
"prefix_condition": "ge"
}
],
"prefix_name": "MPLS-LOOPBACK"
},
{
"prefix_entry":
[
{
"prefix": "100.92.57.168/30",
"prefix_match": "",
"prefix_condition": ""
}
],
"prefix_name": "PS_S1"
}
]
Please help me to get this desired output.
I have tried this code:
fsm_results = [['103.29.45.128/27', '32', 'ge', 'MPLS-LOOPBACK'],
['100.127.0.0/23', '32', 'ge', 'MPLS-LOOPBACK'],
['100.92.57.168/30', '', '', 'PS_S1']
]
prefix_name = []
for i in fsm_results:
prefix_name1 = i[3]
if prefix_name1 not in prefix_name:
prefix_name.append(prefix_name1)
temp1 = []
for i in range(0,len(prefix_name)):
temp = {"prefix_name": prefix_name[i]}
for j in range(0,len(fsm_results)):
if temp['prefix_name'] == fsm_results[j][3]:
b = {"prefix":fsm_results[j][0],'prefix_match':fsm_results[j][1],'prefix_condition':fsm_results[j][2]}
temp.update(b)
temp1.append(temp)
print(temp1)
And i am getting output like this :
[{'prefix_name': 'MPLS-LOOPBACK', 'prefix': '100.127.0.0/23', 'prefix_match': '32', 'prefix_condition': 'ge'}, {'prefix_name': 'PS_S1', 'prefix': '100.92.57.168/30', 'prefix_match': '', 'prefix_condition': ''}]
Upvotes: 3
Views: 93
Reputation: 520
Solution using setdefault and zip:
d = {}
for i in input:
# Used setdefault method to get list of values with same index. Used zip function to group the values.
d.setdefault(i[-1],[]).append(dict(zip(['prefix', 'prefix_match', 'prefix_condition'],i[:-1])))
res = {"prefix_set": [{"prefix_name":i ,"prefix_entry": v } for i, v in d.items()]}
print (res)
# Output:
{'prefix_set': [{'prefix_name': 'MPLS-LOOPBACK', 'prefix_entry': [{'prefix': '103.29.45.128/27', 'prefix_match': '32', 'prefix_condition': 'ge'}, {'prefix': '100.127.0.0/23', 'prefix_match': '32', 'prefix_condition': 'ge'}]}, {'prefix_name': 'PS_S1', 'prefix_entry': [{'prefix': '100.92.57.168/30', 'prefix_match': '', 'prefix_condition': ''}]}]}
I hope this counts.
Upvotes: 1
Reputation: 18106
You could make use of defaultdict to shorten your code and just loop twice:
from collections import defaultdict
res = defaultdict(list)
fsm_results = [['103.29.45.128/27', '32', 'ge', 'MPLS-LOOPBACK'], ['100.127.0.0/23', '32', 'ge', 'MPLS-LOOPBACK'], ['100.92.57.168/30', '', '', 'PS_S1']]
for fsm in fsm_results:
name = fsm.pop()
item = dict(zip(['prefix', 'prefix_match', 'prefix_condition'], fsm))
res[name].append(item)
result = {'prefix_set': [{'prefix_name': k, 'prefix_entry': v} for (k, v) in res.items()]}
print result
Output:
{'prefix_set': [{'prefix_entry': [{'prefix': '103.29.45.128/27',
'prefix_condition': 'ge',
'prefix_match': '32'},
{'prefix': '100.127.0.0/23',
'prefix_condition': 'ge',
'prefix_match': '32'}],
'prefix_name': 'MPLS-LOOPBACK'},
{'prefix_entry': [{'prefix': '100.92.57.168/30',
'prefix_condition': '',
'prefix_match': ''}],
'prefix_name': 'PS_S1'}]}
Upvotes: 1
Reputation: 140
This is happening because you are using update
instead of appending to the list in temp
?
Some other pointers: Do not use range(len(somthing))
. The better way is to use enumerate(somthing)
. enumerate
returns both the index and the item.
Always use meaningful variable names. This will help you later when you or someone else re-reads your code and tries to understand it.
Upvotes: 0
Reputation: 357
I have edited your code and getting the output as per your requirement.
fsm_results = [['103.29.45.128/27', '32', 'ge', 'MPLS-LOOPBACK'],
['100.127.0.0/23', '32', 'ge', 'MPLS-LOOPBACK'],
['100.92.57.168/30', '', '', 'PS_S1']
]
prefix_name = []
for i in fsm_results:
prefix_name1 = i[3]
if prefix_name1 not in prefix_name:
prefix_name.append(prefix_name1)
temp1 = {"prefix_sets":[]}
for i in range(0,len(prefix_name)):
temp = {"prefix_name": prefix_name[i],'prefix_entry':[]}
for j in range(0,len(fsm_results)):
if temp['prefix_name'] == fsm_results[j][3]:
b = {"prefix":fsm_results[j][0],'prefix_match':fsm_results[j][1],'prefix_condition':fsm_results[j][2]}
temp['prefix_entry'].append(b)
temp1["prefix_sets"].append(temp)
print(temp1)
The output is
{'prefix_sets': [{'prefix_name': 'MPLS-LOOPBACK', 'prefix_entry': [{'prefix': '103.29.45.128/27', 'prefix_match': '32', 'prefix_condition': 'ge'}, {'prefix': '100.127.0.0/23', 'prefix_match': '32', 'prefix_condition': 'ge'}]}, {'prefix_name': 'PS_S1', 'prefix_entry': [{'prefix': '100.92.57.168/30', 'prefix_match': '', 'prefix_condition': ''}]}]}
Upvotes: 2