Reputation: 1459
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 i.e. 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.
Upvotes: 0
Views: 186
Reputation: 2882
Extract the priority and keys and then sort based on priority i.e.
sorted_dict.keyOrder = [key[1] for key in sorted((v['priority'],k) for k,v in sorted_dict.items())]
Upvotes: 1