Python:按值对嵌套字典进行排序 [英] Python: Sort nested dictionary by value
问题描述
我正在使用一个大型词典(500多个键),该词典具有2个字典(具有其值的数字)和两个列表(一个具有length =〜10的数字,一个具有length =〜的布尔值) 50).我想获得某种表示形式,无论是列表还是元组,它都通过一个内部词典之一内部的数值之一对原始词典进行排序.有没有办法做到这一点?
I am working with a large dictionary (500+ keys) that has as its values 2 dictionaries (that have as their values numerics) and two lists (one of numerics with length=~10, one of bools with length=~50). I would like to get some sort of representation, be it a list or tuple, that sorts the original dictionary by one of the numeric values inside of one of the inner dictionaries. Is there a way to do this?
作为参考,这是我正在使用的(大)词典中键"WHR"的单个条目:
For reference, here is a single entry for key 'WHR' from the (large) dictionary I am working with:
dd_rf['WHR'] = {'counts': {'num': 81.0,
'numClassCorrect': 64,
'numClassDown': 20,
'numClassDownCorrect': 20,
'numClassIncorrect': 17.0,
'numClassUp': 61,
'numClassUpCorrect': 44,
'numDown': 37,
'numUp': 44},
'imp': array([ 0.50924113, 0.06348138, 0.07851569, 0.03005051, 0.04103215,
0.05646218, 0.03682958, 0.03642228, 0.04282599, 0.01439416,
0.03090185, 0.0598431 ]),
'rates': {'correctDownRate': 1.0,
'correctRate': 0.7901234567901234,
'correctUpRate': 0.7213114754098361,
'downRate': 0.4567901234567901,
'upRate': 0.5432098765432098},
'results': array([False, False, True, True, True, True, False, False, True,
False, True, True, False, False, True, False, True, True,
True, True, True, True, True, True, True, False, False,
True, True, True, True, True, True, True, True, True,
True, False, True, False, False, True, True, True, True,
True, True, True, True, False, True, False, True, True,
True, True, True, False, False, False, True, True, True,
True, True, True, True, True, True, True, False, True,
True, True, False, True, True, True, True, True, True], dtype=bool)}
因此,在我的工作中,我想按降序将整个字典dd_rf按dd_rf [key] ['rates'] ['correctRate']的值排序.在上面的示例中,与给定排序标准相对应的值为'correctRate':0.7901234567901234.
So, in the context of my work, I would like to sort the entire dictionary 'dd_rf' by the values of 'dd_rf[key]['rates']['correctRate'] in descending order. The value corresponding to the given sort criteria in the above example is 'correctRate': 0.7901234567901234.
谢谢!请让我知道是否可以提供更多背景信息.
Thanks! Please let me know if I can provide any more background information.
推荐答案
我不确定您要查找的最终形式是什么,因为将结果塞回到字典中会失去顺序.也许看看collections.OrderedDict.但是,基本思想是将字典变成一个列表,然后使用自定义函数对该列表进行排序.例如
I'm not sure what the final form you're looking for is, since stuffing the result back into a dictionary will lose the ordering. Perhaps look into collections.OrderedDict. However, the basic idea is to just turn your dictionary into a list and then sort that list with your custom function. e.g.
answer = sorted(dd_rf.items(), key=lambda (k,v): v['rates']['correctRate'])
这是一般想法.您可能想先看看类似的东西
That's the general idea. You may want to start by seeing how something like
sorted(range(10), key=lambda x: (x-5)**2)
努力建立您的理解.
这篇关于Python:按值对嵌套字典进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!