Sort dictionary of dictionaries by value Python ( 3 Level Dict ) [closed]

天大地大妈咪最大 提交于 2021-02-11 18:05:26

问题


I have a dict like this:

{
 '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}
    }
}

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


回答1:


Something like this would work, but I think If you are starting with python, you should learn about the sorted function in python.

>>> sorted(k.items(), key = lambda x: x[1][0].itervalues().next()['val'])

Where k is your dictionary.

Link provided by viki.omega9 in his answer should be a good starting point.

Note: The command will return a list of tuples (or pairs) in sorted order of the form:

[(key1, val1), (key2, val2)]

because dictionaries by nature are unordered.




回答2:


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.

For example,

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

EDIT: 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.

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

Given this,

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


来源:https://stackoverflow.com/questions/29853024/sort-dictionary-of-dictionaries-by-value-python-3-level-dict

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