python - Dictionary of Dictionaries : Sorting by a specific key -


i have dictionary looks this

{'africa': {'name': 'africa',             'men': 33333,             'priority': 3,             'women': 30000},  'america': {'name': 'usa',              'men': 1114444411333l,              'priority': 4,              'women': 44430000},  'asia': {'name': 'china',           'men': 444433333,           'priority': 2,           'women': 444430000},  'europe': {'name': 'uk',             'men': 11111333,             'priority': 1,             'women': 1111430000}} 

i need sort dictionary key = priority

i'm using 2.7 , have tried few options (which dont elegant). suggestions?

>>> d = {"africa" :       { "name" : "africa", "men": 33333, "women" : 30000, "priority" :3},  "asia":       { "name" : "china", "men": 444433333, "women" : 444430000, "priority" :2},  "europe":       { "name" : "uk", "men": 11111333, "women" : 1111430000, "priority" :1},  "america":       { "name" : "usa", "men": 1114444411333, "women" : 44430000, "priority" :4} } >>> collections import ordereddict >>> ordereddict(sorted(d.items(), key=lambda x: x[1]['priority'])) ordereddict([('europe', {'priority': 1, 'men': 11111333, 'name': 'uk', 'women': 1111430000}), ('asia', {'priority': 2, 'men': 444433333, 'name': 'china', 'women': 444430000}), ('africa', {'priority': 3, 'men': 33333, 'name': 'africa', 'women': 30000}), ('america', {'priority': 4, 'men': 1114444411333l, 'name': 'usa', 'women': 44430000})]) 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -