Python-如何根据字典的值及其键对字典进行排序 [英] Python - How to sort a dictionary based on its value as well it key

查看:63
本文介绍了Python-如何根据字典的值及其键对字典进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字典,如:-

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

...我希望按照元组键的第二个字段以及字典的value字段对其进行排序...因此结果字典应为:-

...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   }

我想输出...

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

....正如我所说,我想将元组 second 的值用于排序,而不要考虑将第一个元组的值用于排序...如果有人能够告诉,这将是一个很大的帮助..我如何从已排序的字典中将元组提取到类似..

....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)

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

,因此该评论有助于获得我想要的...非常感谢...

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])

更正了键,值对,感谢@Padraic Cunningham指出

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

推荐答案

字典没有顺序,您可以对项目进行排序:

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.

这篇关于Python-如何根据字典的值及其键对字典进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆