Python - How to sort a dictionary based on its value as well it key

拥有回忆 提交于 2021-01-29 17:01:30

问题


I have a dictionary as :-

{
 (1, 1): 16,
 (1, 2): 16,
 (1, 3): 16,
 (1, 4): 16,
 (2, 1): 24,
 (2, 2): 24,
 (2, 3): 24,
 (2, 4): 24   }

...i want it to be ordered as per tuple key's second field as well as dictionary's value field... so result dictionary should be :-

{
 (2, 1): 24,
 (2, 2): 24,
 (2, 3): 24,
 (2, 4): 24, 
 (1, 1): 16,
 (1, 2): 16,
 (1, 3): 16,
 (1, 4): 16 
}

... i tried sorting it with value alone but it messes the keys tuple order.... yes so i assigned it to a list afterwards by doing ...

list = sorted(unsorted_dict, key = unsorted_dict.__getitem__, reverse=True)....

Sorry to say but answer address my situation partially ...

i dont want to consider first value for sorting..lets say i had initial data ....

{
 (5, 1): 16,
 (6, 2): 16,
 (7, 3): 16,
 (8, 4): 16,
 (4, 1): 24,
 (3, 2): 24,
 (2, 3): 24,
 (1, 4): 24   }

and i wanted output ...

{
     (4, 1): 24,
     (3, 2): 24,
     (2, 3): 24,
     (1, 4): 24,     
     (5, 1): 16,
     (6, 2): 16,
     (7, 3): 16,
     (8, 4): 16        }

....as i said i want to consider tuples second value for sorting and not consider at all first value for sorting...Also it would be a great help if someone can tell ..how do i extract the tuples from this sorted dictionary to a list like ..

(4, 1),(3, 2),(2, 3),(1, 4),(5, 1),(6, 2),(7, 3),(8, 4)

EDIT: so the comment helped to get what i wanted ...thanks a lot ...

list_sorted = sorted(unsorted_dict.items(),key=lambda x: (-x[1],x[0][1])) 
for val in list_sorted: 
   final_list.append(val[0])

corrected the key,value pairs thanks @Padraic Cunningham for pointing it out


回答1:


dicts have no order, you can sort the items:

d={
 (1, 1): 16,
 (1, 2): 16,
 (1, 3): 16,
 (1, 4): 16,
 (2, 1): 24,
 (2, 2): 24,
 (2, 3): 24,
 (2, 4): 24   }

from pprint import pprint as pp

pp(sorted(d.items(),key=lambda x: (-x[1],x[0])))
[((2, 1), 24),
 ((2, 2), 24),
 ((2, 3), 24),
 ((2, 4), 24),
 ((1, 1), 16),
 ((1, 2), 16),
 ((1, 3), 16),
 ((1, 4), 16)]

And if you really want a dict with the items in order you can create a collections.OrderedDict from the sorted items:

from collections import OrderedDict

od = OrderedDict(sorted(d.items(),key=lambda x: (-x[1],x[0])))
OrderedDict([((2, 1), 24), ((2, 2), 24), ((2, 3), 24), ((2, 4), 24), ((1, 1), 16), ((1, 2), 16), ((1, 3), 16), ((1, 4), 16)])

Our primary sort key is each value, sorting from high to low by negating each value with a -, then we break ties with the key/tuple sorting from low to high.

Break ties by second element in tuple:

pp(sorted(d.items(),key=lambda x:(-x[1],x[0][1])))

Your edited output now has different keys with different values so not sure how sort can do that.



来源:https://stackoverflow.com/questions/28762489/python-how-to-sort-a-dictionary-based-on-its-value-as-well-it-key

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!