按值对字典字典进行排序Python(3级字典) [英] Sort dictionary of dictionaries by value Python ( 3 Level Dict )

查看:64
本文介绍了按值对字典字典进行排序Python(3级字典)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的 dict :

{
 'INT-ABC1': 
    {
        'acc1': {'val': -22313.7381693064, 'Qua': -241.0}, 
        'acc2': {'val': -1312.940854148, 'Qua': -13.0}
    }, 
 'INT-ABC2': 
    {
        'acc1': {'val': -131.2510359399, 'Qua' : -23.0}, 
        'acc3': {'val': -131.40521409002, 'Qua' : -13.0},
        'acc5': {'val': -12312.7688190937, 'Qua' : -1313.0}
    }
}

我需要按'val'对其进行排序,并从中得到类似的指示.

I need to sort it by 'val' and get a similar dict out of it.

推荐答案

考虑使用排序函数.使用作为参考.对于这种情况,您必须使用一个关键函数,该函数告诉python如何比较问题中字典的两个实体.然后,排序后的函数将返回您要迭代的列表.

Consider using the sorted function. Use this as a reference. For the case that you have, you have to use a key function that tells python how to compare two entities of the dictionary you have in your question. The sorted function then returns a list that you would iterate.

例如

>>>sorted(student_tuples, key=lambda student: student[2])   # sort by age
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]

对于您的情况,您需要从字典中获取项目.

For your case you need to get the items from the dict.

首先,就像其中一条评论所说的那样,您不能直接在python中对字典进行排序.我认为您不需要的是命令式的字典.您只能对已转换为列表元组的字典进行排序.例如{'a':'b','c':'d'},必须先转换为[('a','b'),('c','d')],然后再转换为可以选择想要的排序方式.这里的问题是,一旦执行此操作,就无法将其存储回普通字典中.原因是这不是正常命令的工作方式.您可以在python集合中使用ordereddict,以记住"插入顺序,因此是排序的" dict.但是,如果不做一些工作,就无法轻松地创建排序的"字典.

First like one of the comments said, you can't directly sort a dict in python. I don't think an ordered dict is what you need. You can only sort a dict that has been converted to a list tuples. For example {'a': 'b', 'c': 'd'}, has to be first converted to [('a', 'b'), ('c', 'd')] and then you can choose to sort as you wish. The problem here is that once you do this, you can't store it back into a normal dict. The reason is that's not how normal dicts in work. You can use an ordereddict in the python collections that "remembers" the order of insert and therefore is a "sorted" dict. But without doing some work you cannot easily create a "sorted" dict.

在您的输入中,本例中的'b'是字典.因此,您必须做一些工作才能获得所需的密钥.我假设您想在每个"INT-ABC"的"acc"值之间进行排序.

For your input, the 'b' in my example is a dict. So you have to do some work to get the key that you want. I am assuming you want to sort between the 'acc' values for each 'INT-ABC'.

>>> k_dict
{'INT-ABC1': {'acc1': {'Qua': -241.0, 'val': -22313.7381693064}, 'acc2': {'Qua': -13.0, 'val': -1312.940854148}}}
>>> k_dict.items()
[('INT-ABC1', {'acc1': {'Qua': -241.0, 'val': -22313.7381693064}, 'acc2': {'Qua': -13.0, 'val': -1312.940854148}})]
>>> k_dict.items()[0][1].items()
[('acc1', {'Qua': -241.0, 'val': -22313.7381693064}), ('acc2', {'Qua': -13.0, 'val': -1312.940854148})]
>>> sorted(k_dict.items()[0][1].items(), key=lambda  x: x[1]['val'])
[('acc1', {'Qua': -241.0, 'val': -22313.7381693064}), ('acc2', {'Qua': -13.0, 'val': -1312.940854148})]

给出这个,

>>> from collections import OrderedDict
>>> t = OrderedDict()
>>> k_dict.items()[0][0]
'INT-ABC1'
>>> t[k_dict.items()[0][0]] = sorted(k_dict.items()[0][1].items(), key=lambda  x: x[1]['val'])
>>> t
OrderedDict([('INT-ABC1', [('acc1', {'Qua': -241.0, 'val': -22313.7381693064}), ('acc2', {'Qua': -13.0, 'val': -1312.940854148})])])

这篇关于按值对字典字典进行排序Python(3级字典)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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