Python按大多数值排序dicts

时间:2017-10-15 06:52:38

标签: python sorting dictionary

我正在寻找一种方法,通过包含最多值的字典来对字典进行排序。  这是原始的词典:

test = {u'Beta': {
        u'SW Engineering': {
                u'Resolved': {u'2017-10-06 08:04:15': 1, u'2017-10-15 00:19:35': 3, u'2017-10-11 00:19:29': 2, u'2017-10-09 00:19:00': 1, u'2017-10-14 00:21:09': 3, u'2017-10-12 00:19:52': 3, u'2017-10-08 00:18:58': 1, u'2017-10-10 00:19:31': 2, u'2017-10-13 00:19:28': 3, u'2017-10-07 09:54:33': 1},
                u'In Progress': {u'2017-10-06 08:04:15': 9, u'2017-10-15 00:19:35': 12, u'2017-10-11 00:19:29': 11, u'2017-10-09 00:19:00': 9, u'2017-10-14 00:21:09': 12, u'2017-10-12 00:19:52': 11, u'2017-10-08 00:18:58': 9, u'2017-10-10 00:19:31': 10, u'2017-10-13 00:19:28': 11, u'2017-10-07 09:54:33': 9},
                u'In Testing': {u'2017-10-06 08:04:15': 7, u'2017-10-15 00:19:35': 7, u'2017-10-11 00:19:29': 7, u'2017-10-09 00:19:00': 7, u'2017-10-14 00:21:09': 7, u'2017-10-12 00:19:52': 7, u'2017-10-08 00:18:58': 7, u'2017-10-10 00:19:31': 7, u'2017-10-13 00:19:28': 7, u'2017-10-07 09:54:33': 7},
                u'Reopened': {u'2017-10-06 08:04:15': 1, u'2017-10-15 00:19:35': 1, u'2017-10-11 00:19:29': 1, u'2017-10-09 00:19:00': 1, u'2017-10-14 00:21:09': 1, u'2017-10-12 00:19:52': 1, u'2017-10-08 00:18:58': 1, u'2017-10-10 00:19:31': 1, u'2017-10-13 00:19:28': 1, u'2017-10-07 09:54:33': 1},
                u'Closed': {u'2017-10-06 08:04:15': 17, u'2017-10-15 00:19:35': 18, u'2017-10-11 00:19:29': 18, u'2017-10-09 00:19:00': 17, u'2017-10-14 00:21:09': 18, u'2017-10-12 00:19:52': 18, u'2017-10-08 00:18:58': 17, u'2017-10-10 00:19:31': 18, u'2017-10-13 00:19:28': 18, u'2017-10-07 09:54:33': 17},
                u'Open': {u'2017-10-06 08:04:15': 5, u'2017-10-15 00:19:35': 8, u'2017-10-11 00:19:29': 8, u'2017-10-09 00:19:00': 5, u'2017-10-14 00:21:09': 8, u'2017-10-12 00:19:52': 7, u'2017-10-08 00:18:58': 5, u'2017-10-10 00:19:31': 8, u'2017-10-13 00:19:28': 8, u'2017-10-07 09:54:33': 5}},
        u'DSP Engineering': {
                u'In Progress': {u'2017-10-15 00:19:35': 1, u'2017-10-14 00:21:09': 1}}}}


这就是我想要的样子:

test = {u'Beta': {
        u'DSP Engineering': {
                u'In Progress': {u'2017-10-15 00:19:35': 1, u'2017-10-14 00:21:09': 1}},
        u'SW Engineering': {
                u'Resolved': {u'2017-10-06 08:04:15': 1, u'2017-10-15 00:19:35': 3, u'2017-10-11 00:19:29': 2, u'2017-10-09 00:19:00': 1, u'2017-10-14 00:21:09': 3, u'2017-10-12 00:19:52': 3, u'2017-10-08 00:18:58': 1, u'2017-10-10 00:19:31': 2, u'2017-10-13 00:19:28': 3, u'2017-10-07 09:54:33': 1},
                u'In Progress': {u'2017-10-06 08:04:15': 9, u'2017-10-15 00:19:35': 12, u'2017-10-11 00:19:29': 11, u'2017-10-09 00:19:00': 9, u'2017-10-14 00:21:09': 12, u'2017-10-12 00:19:52': 11, u'2017-10-08 00:18:58': 9, u'2017-10-10 00:19:31': 10, u'2017-10-13 00:19:28': 11, u'2017-10-07 09:54:33': 9},
                u'In Testing': {u'2017-10-06 08:04:15': 7, u'2017-10-15 00:19:35': 7, u'2017-10-11 00:19:29': 7, u'2017-10-09 00:19:00': 7, u'2017-10-14 00:21:09': 7, u'2017-10-12 00:19:52': 7, u'2017-10-08 00:18:58': 7, u'2017-10-10 00:19:31': 7, u'2017-10-13 00:19:28': 7, u'2017-10-07 09:54:33': 7},
                u'Reopened': {u'2017-10-06 08:04:15': 1, u'2017-10-15 00:19:35': 1, u'2017-10-11 00:19:29': 1, u'2017-10-09 00:19:00': 1, u'2017-10-14 00:21:09': 1, u'2017-10-12 00:19:52': 1, u'2017-10-08 00:18:58': 1, u'2017-10-10 00:19:31': 1, u'2017-10-13 00:19:28': 1, u'2017-10-07 09:54:33': 1},
                u'Closed': {u'2017-10-06 08:04:15': 17, u'2017-10-15 00:19:35': 18, u'2017-10-11 00:19:29': 18, u'2017-10-09 00:19:00': 17, u'2017-10-14 00:21:09': 18, u'2017-10-12 00:19:52': 18, u'2017-10-08 00:18:58': 17, u'2017-10-10 00:19:31': 18, u'2017-10-13 00:19:28': 18, u'2017-10-07 09:54:33': 17},
                u'Open': {u'2017-10-06 08:04:15': 5, u'2017-10-15 00:19:35': 8, u'2017-10-11 00:19:29': 8, u'2017-10-09 00:19:00': 5, u'2017-10-14 00:21:09': 8, u'2017-10-12 00:19:52': 7, u'2017-10-08 00:18:58': 5, u'2017-10-10 00:19:31': 8, u'2017-10-13 00:19:28': 8, u'2017-10-07 09:54:33': 5}}}}

2 个答案:

答案 0 :(得分:1)

如何使用OrderedDict进行示例。

from collections import OrderedDict
import json


test = {u'Beta': {
        u'SW Engineering': {
                u'Resolved': {u'2017-10-06 08:04:15': 1, u'2017-10-15 00:19:35': 3, u'2017-10-11 00:19:29': 2, u'2017-10-09 00:19:00': 1, u'2017-10-14 00:21:09': 3, u'2017-10-12 00:19:52': 3, u'2017-10-08 00:18:58': 1, u'2017-10-10 00:19:31': 2, u'2017-10-13 00:19:28': 3, u'2017-10-07 09:54:33': 1},
                u'In Progress': {u'2017-10-06 08:04:15': 9, u'2017-10-15 00:19:35': 12, u'2017-10-11 00:19:29': 11, u'2017-10-09 00:19:00': 9, u'2017-10-14 00:21:09': 12, u'2017-10-12 00:19:52': 11, u'2017-10-08 00:18:58': 9, u'2017-10-10 00:19:31': 10, u'2017-10-13 00:19:28': 11, u'2017-10-07 09:54:33': 9},
                u'In Testing': {u'2017-10-06 08:04:15': 7, u'2017-10-15 00:19:35': 7, u'2017-10-11 00:19:29': 7, u'2017-10-09 00:19:00': 7, u'2017-10-14 00:21:09': 7, u'2017-10-12 00:19:52': 7, u'2017-10-08 00:18:58': 7, u'2017-10-10 00:19:31': 7, u'2017-10-13 00:19:28': 7, u'2017-10-07 09:54:33': 7},
                u'Reopened': {u'2017-10-06 08:04:15': 1, u'2017-10-15 00:19:35': 1, u'2017-10-11 00:19:29': 1, u'2017-10-09 00:19:00': 1, u'2017-10-14 00:21:09': 1, u'2017-10-12 00:19:52': 1, u'2017-10-08 00:18:58': 1, u'2017-10-10 00:19:31': 1, u'2017-10-13 00:19:28': 1, u'2017-10-07 09:54:33': 1},
                u'Closed': {u'2017-10-06 08:04:15': 17, u'2017-10-15 00:19:35': 18, u'2017-10-11 00:19:29': 18, u'2017-10-09 00:19:00': 17, u'2017-10-14 00:21:09': 18, u'2017-10-12 00:19:52': 18, u'2017-10-08 00:18:58': 17, u'2017-10-10 00:19:31': 18, u'2017-10-13 00:19:28': 18, u'2017-10-07 09:54:33': 17},
                u'Open': {u'2017-10-06 08:04:15': 5, u'2017-10-15 00:19:35': 8, u'2017-10-11 00:19:29': 8, u'2017-10-09 00:19:00': 5, u'2017-10-14 00:21:09': 8, u'2017-10-12 00:19:52': 7, u'2017-10-08 00:18:58': 5, u'2017-10-10 00:19:31': 8, u'2017-10-13 00:19:28': 8, u'2017-10-07 09:54:33': 5}},
        u'DSP Engineering': {
                u'In Progress': {u'2017-10-15 00:19:35': 1, u'2017-10-14 00:21:09': 1}}}}


engineers_tasks = []
for engineer, tasks in test['Beta'].items():
    tasks_sorted = sorted(tasks.items(), key=lambda k: len(k[1]), reverse=True)
    min_tasks = len(tasks_sorted[-1][1])
    engineers_tasks.append((engineer, OrderedDict(tasks_sorted), min_tasks))

engineers_tasks = sorted(engineers_tasks, key=lambda k: k[-1])
engineers_tasks = OrderedDict([tasks[:-1] for tasks in engineers_tasks])

test_sorted = {'Beta': engineers_tasks}
print json.dumps(test_sorted, indent=2)

答案 1 :(得分:-1)

这不是最佳解决方案,但您可以尝试。 出于测试目的,我在dict中添加了另外一个项目,其中包含名为" SW Engineering33"

的最大元素
package Bank;
    public class Bank{
    //some methods
    //some code
    }
    class Main{//Calling this class from another .java file.
    //some methods
    public void getRandom()
    {
    return somenumber;
    }
    }