My source list looks like this :
source =
[['Ans1','Section1','Type2'],['Ans2','Section1','Type2'],['Ans3','Section2','Type2'],['Ans4','Section1','Type1']]
I would need to create my target in the following format:
{
"Type2":{
"0":{
"Ans":"Ans1",
"Section":"Section1"
},
"1":{
"Ans":"Ans2",
"Section":"Section1"
},
"2":{
"Ans":"Ans3",
"Section":"Section2"
}},
"Type1":{
"0":{
"Ans":"Ans4",
"Section":"Section1"
}
},
"Type3":{
},
"Type4":{
},
"Type5":{
}
}
So, last value from the dictionary will become the keys and the order is maintained. Say, for Type2 I have three answers. Hence my target has key as Type2 and it has three inner dictionaries with 0,1,2.
I have already created my empty target dictionary (Type3, Type4, Type5 are empty are there no records for these types in my source) :
empty_target =
{
"Type1":{
"0":{
"Ans":"",
"Section":""
},
"1":{
"Ans":"",
"Section":""
},
"2":{
"Ans":"",
"Section":""
}},
"Type2":{
"0":{
"Ans":"",
"Section":""
}
},
"Type3":{
},
"Type4":{
},
"Type5":{
}
}
Can anyone assist me how can I populate empty_target with source? Thanks.
Edit: Added the code to create the empty target:
#Sorting my source data
import operator
source = [['Ans1','Section1','Type2'],['Ans2','Section1','Type2'],['Ans3','Section2','Type2'],['Ans4','Section1','Type1']]
new_ = sorted(source, key = operator.itemgetter(0,2))
print(new_)
[['Ans1', 'Section1', 'Type2'], ['Ans2', 'Section1', 'Type2'], ['Ans3', 'Section2', 'Type2'], ['Ans4', 'Section1', 'Type1']]
#Getting the count of Types
doctype_list = [d[2] for d in new]
docs_count_dict = {}
for x in set(doctype_list):
docs_count_dict[x] = doctype_list.count(x)
print(docs_count_dict)
{'Type2': 3, 'Type1': 1}
#Sorting the type based on new_ list
sorted_docs = sorted(set(doctype_list),key=doctype_list.index)
sorted_docs_orig = sorted_docs.copy()
print (sorted_docs_orig)
['Type2', 'Type1']
tot_types = ['Type1','Type2','Type3','Type4','Type5']
#Sorting the types out of total types : Note Type2 is ordered first as it is the first type in new_
for val in tot_types:
if val in sorted_docs:
continue
sorted_docs.append(val)
print(sorted_docs)
['Type2', 'Type1', 'Type3', 'Type4', 'Type5']
#Creating my empty target:
api_res = {}
for val in sorted_docs:
api_res[val] = {}
print (api_res)
{'Type2': {}, 'Type1': {}, 'Type3': {}, 'Type4': {}, 'Type5': {}}
cols = ['Ans','Section']
for key,val in api_res.items():
if key in docs_count_dict:
for i in range(docs_count_dict[key]):
val[i] = dict.fromkeys(cols, "")
print (api_res)
{'Type2': {0: {'Ans': '', 'Section': ''},
1: {'Ans': '', 'Section': ''},
2: {'Ans': '', 'Section': ''}},
'Type1': {0: {'Ans': '', 'Section': ''}},
'Type3': {},
'Type4': {},
'Type5': {}}