The question is how to parse dictionary by key extract values based on max and low value. Then compare values in it, and add some key, value based on calculation. Maybe the question is messy but I will provide more explanation with code.
json object
[
{
"YCI": 0.014076729909151329,
"TCI": 0.5610661498232027,
"FCI": 0.327632092582722,
"TFCI": 3.04906430661623,
"naziv": "HDZ",
"YCSI": 14.49723089452966
},
{
"YCI": 0.04249999676677044,
"TCI": 0.33129318126147167,
"FCI": 1.677073944380761,
"TFCI": 4.326953034001479,
"naziv": "HNS",
"YCSI": 56.80909574468085
},
{
"YCI": 0,
"TCI": 0.40603351151808614,
"FCI": 0,
"TFCI": 12.61045547126369,
"naziv": "HSP AS",
"YCSI": 0
},
{
"YCI": 2.231205214448367,
"TCI": 0,
"FCI": 0,
"TFCI": 0,
"naziv": "HSS",
"YCSI": 949.343111111111
}
]
Ok, this is my dictionary. Now how to compare keys in dictionary:
- YCI
- TCI
- FCI
- TFCI
- naziv
- YSCI
with each other based on their values. If value YCI has highest value then YCI, it should set number 1 in the new dictionary, and that logic is also applied to other keys in dictionary. The output would be something like:
json-output
[
{
"YCI": 3,
"TCI": 1,
"FCI": 2,
"TFCI": 3,
"naziv": "HDZ",
"YCSI": 3
},
{
"YCI": 2,
"TCI": 3,
"FCI": 1,
"TFCI": 2,
"naziv": "HNS",
"YCSI": 2
},
{
"YCI": 0,
"TCI": 2,
"FCI": 0,
"TFCI": 1,
"naziv": "HSP AS",
"YCSI": 0
},
{
"YCI": 1,
"TCI": 0,
"FCI": 0,
"TFCI": 0,
"naziv": "HSS",
"YCSI": 1
}
]
The highest value gets number 1 and the lowest value depends on how many objects dictionary contains.
code-try
print max(nvalues[0].iteritems(), key=operator.itemgetter(1))[1]
With this code I can extract the highest value, but how to sort dictionary within its values. If you could give me some logic to solve this, I would appreciate.