Sorting by nested dictionary in Python dictionary -


i have below structure

{     'searchresult' : [{             'resulttype' : 'station',             'ranking' : 0.5         }, {             'resulttype' : 'station',             'ranking' : 0.35         }, {             'resulttype' : 'station',             'ranking' : 0.40         }     ] } 

and want get

{     'searchresult' : [{             'resulttype' : 'station',             'ranking' : 0.5         }, {             'resulttype' : 'station',             'ranking' : 0.4         }, {             'resulttype' : 'station',             'ranking' : 0.35         }     ] } 

tried code without success

result = sorted(result.items(), key=lambda k: k[1][0][1]["ranking"], reverse=true) 

if okay changing objects in-place.

a = {     'searchresult' : [{                        'resulttype' : 'station',                        'ranking' : 0.5                       }, {                        'resulttype' : 'station',                        'ranking' : 0.35                       }, {                       'resulttype' : 'station',                       'ranking' : 0.40                       }]   }  a["searchresult"].sort(key=lambda d: d["ranking"], reverse=true) 

or can make deep copy keep original

from copy import deepcopy   srt_dict = deepcopy(a) srt_dict["searchresult"].sort(key=lambda d: d["ranking"], reverse=true) 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

python - build a suggestions list using fuzzywuzzy -