I am trying to find a best-case algorithm to make the nested list into a JSON.
Nested List
[["No","Ratios","2018","2019","2020"],["Profitability Ratio","Profitability Ratio","Profitability Ratio","Profitability Ratio","Profitability Ratio"],["1","Return on Assets (%)","-0.50","0.86","26.62"],["2","Return on Equity (%)","-0.51","0.87","27.14"],["3","Gross Profit Margin (%)","0.00","0.00","0.00"],["Liquidity Ratio","Liquidity Ratio","Liquidity Ratio","Liquidity Ratio","Liquidity Ratio"],["4","Current Ratio (times)","-0.50","0.86","26.62"],["5","Accounts Receivable Turnover (times)","-0.51","0.87","27.14"],["Operation Efficiency Ratio","Operation Efficiency Ratio","Operation Efficiency Ratio","Operation Efficiency Ratio","Operation Efficiency Ratio"],["6","Total Assets Turnover (times)","-0.50","0.86","26.62"],["7","Operation Expense to Total Revenue Ratio (%)","-0.51","0.87","27.14"]]
Beautified Nested List
[
[
"No",
"Ratios",
"2018",
"2019",
"2020"
],
[
"Profitability Ratio",
"Profitability Ratio",
"Profitability Ratio",
"Profitability Ratio",
"Profitability Ratio"
],
[
"1",
"Return on Assets (%)",
"-0.50",
"0.86",
"26.62"
],
[
"2",
"Return on Equity (%)",
"-0.51",
"0.87",
"27.14"
],
[
"3",
"Gross Profit Margin (%)",
"0.00",
"0.00",
"0.00"
],
[
"Liquidity Ratio",
"Liquidity Ratio",
"Liquidity Ratio",
"Liquidity Ratio",
"Liquidity Ratio"
],
[
"4",
"Current Ratio (times)",
"-0.50",
"0.86",
"26.62"
],
[
"5",
"Accounts Receivable Turnover (times)",
"-0.51",
"0.87",
"27.14"
],
[
"Operation Efficiency Ratio",
"Operation Efficiency Ratio",
"Operation Efficiency Ratio",
"Operation Efficiency Ratio",
"Operation Efficiency Ratio"
],
[
"6",
"Total Assets Turnover (times)",
"-0.50",
"0.86",
"26.62"
],
[
"7",
"Operation Expense to Total Revenue Ratio (%)",
"-0.51",
"0.87",
"27.14"
]
]
The above is the Nested list that need to be converted into a JSON
. And below is the written JSON structure which is derived from the nested list.
{
"Head Key":[
{
"Profitability Ratio":[
{
"No":1,
"Ratios":"Return on Assets (%)",
"2018":"-0.50",
"2019":"0.86",
"2020":"26.62"
},
{
"No":2,
"Ratios":"Return on Equity (%)",
"2018":"-0.51",
"2019":"0.87",
"2020":"27.14"
},
{
"No":3,
"Ratios":"Gross Profit Margin (%)",
"2018":"0.00",
"2019":"0.00",
"2020":"0.00"
}
]
},
{
"Liquidity Ratio":[
{
"No":4,
"Ratios":"Current Ratio (times)",
"2018":"-0.50",
"2019":"0.86",
"2020":"26.62"
},
{
"No":5,
"Ratios":"Accounts Receivable Turnover (times)",
"2018":"-0.51",
"2019":"0.87",
"2020":"27.14"
}
]
},
{
"Operation Efficiency Ratio":[
{
"No":6,
"Ratios":"Total Assets Turnover (times)",
"2018":"-0.50",
"2019":"0.86",
"2020":"26.62"
},
{
"No":7,
"Ratios":"Return on Assets (%)",
"2018":"-0.51",
"2019":"0.87",
"2020":"27.14"
}
]
}
]
}
I am finding the best time complexity algorithm for the above problem using python language. Im bit confused what inbuilt function need to be used to solve this problem
CodePudding user response:
lst = [["No","Ratios","2018","2019","2020"],["Profitability Ratio","Profitability Ratio","Profitability Ratio","Profitability Ratio","Profitability Ratio"],["1","Return on Assets (%)","-0.50","0.86","26.62"],["2","Return on Equity (%)","-0.51","0.87","27.14"],["3","Gross Profit Margin (%)","0.00","0.00","0.00"],["Liquidity Ratio","Liquidity Ratio","Liquidity Ratio","Liquidity Ratio","Liquidity Ratio"],["4","Current Ratio (times)","-0.50","0.86","26.62"],["5","Accounts Receivable Turnover (times)","-0.51","0.87","27.14"],["Operation Efficiency Ratio","Operation Efficiency Ratio","Operation Efficiency Ratio","Operation Efficiency Ratio","Operation Efficiency Ratio"],["6","Total Assets Turnover (times)","-0.50","0.86","26.62"],["7","Operation Expense to Total Revenue Ratio (%)","-0.51","0.87","27.14"]]
result = dict()
for i in range(1, len(lst)):
if not lst[i][0].isnumeric():
key = lst[i][0]
result[key] = []
else:
result[key].append({k: v for (k,v) in zip(lst[0], lst[i])})
print(result)