简体   繁体   中英

Sort dictionary of dictionaries by value of lower-level

I am looking to sort a dictionary by the value of a 'lower-level'. For instance if i had the structure:

 salary_data = {"John D": {"wage":10000, "gender":male}, "Peter A":{"wage":12000, "gender":male}, "Emma":{"wage":5000, "gender":female}}

Then i want returned the keys of the initial dictionary sorted by the wage, in the second stage ie ["Emma", "John D", "Peter A"] .

Try something like this:

print sorted(salary_data, key=lambda k:salary_data[k]['wage'])

Output:

['Emma', 'John D', 'Peter A']

Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM