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

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -