简体   繁体   中英

pythonic way to sort a SortedDict

SortedDict is a python dict with an additional attribute which uses as sorting,

sorted_dict = {
               'personnel info': {'name': 'p_info', 'priority': 1},
               'educational info': {'name': 'edu_info', 'priority': 3}
               'additional info': {'name': 'adi_info', 'priority': 2}
}
sorted_dict.keyOrder = ['personnel info', 'educational info', 'additional info']

now we can use this sorted dict in specific ordering wherever we use.

now i have to sort this dict according to priority ie key order should be on priority basis

>>>print sorted_dict.keyOrder
>>> ['personnel info', 'additional info', 'educational info']

any pythonic way (like sorted) to avoid forloop/function here.

note: i already have sortedDict with initial keyOrder.

提取优先级和键,然后根据优先级排序,即

sorted_dict.keyOrder = [key[1] for key in sorted((v['priority'],k) for k,v in sorted_dict.items())]

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