根据python中的字典值对字典进行排序

根据python中的字典值对字典进行排序,python,sorting,dictionary,Python,Sorting,Dictionary,我有这样的嵌套字典: dic = { 1: { 'name': 'alice', 'point': 10 }, 2: { 'name': 'john', 'point': 12 } 3: { 'name': 'mike', 'point': 8 } 4: { 'name' : 'rose',

我有这样的嵌套字典:

dic = {
    1:
    {
        'name': 'alice',
        'point': 10
    },
    2:
    {
        'name': 'john',
        'point': 12
    }
    3:
    {
        'name': 'mike',
        'point': 8
    }
    4:
    {
        'name' : 'rose',
        'point': 16
    }
    5:
    {
        'name': 'ben',
        'point': 5
    }
}
在我的例子中,我需要根据第二级中键“point”的值对字典进行降序排序。。所以结果是这样的:

{
    4:
    {
        'name' : 'rose',
        'point': 16
    },
    2:
    {
        'name': 'john',
        'point': 12
    },
    1:
    {
        'name': 'alice',
        'point': 10
    },
    3:
    {
        'name': 'mike',
        'point': 8
    },
    5:
    {
        'name': 'ben',
        'point': 5
    }
}

有办法吗?谢谢。

不,你不能对“那本字典”排序。字典是无序的。但是,您可以使用嵌套列表或其他人们在注释中建议的内容。对于有序字典,我建议您查看。

正如其他人所提到的,在字典形式中,您无法对这些项目进行排序。然而,这里有一个解决方案,它可以根据您的需要工作,转换为键、值元组,然后按点排序(这是您的输出暗示的,但没有明确说明)


字典没有订单号。字典没有顺序。说“分类字典”是没有意义的。我建议你检查一下你的数据结构。为什么口述中需要第一级数字键?我的意思是,有顺序字典-你不能使用常规字典,你也可以使用嵌套列表
d = {
    4:
    {
        'name' : 'rose',
        'point': 16
    },
    2:
    {
        'name': 'john',
        'point': 12
    },
    1:
    {
        'name': 'alice',
        'point': 10
    },
    3:
    {
        'name': 'mike',
        'point': 8
    },
    5:
    {
        'name': 'ben',
        'point': 5
    }
}

d_sorted = sorted(d.items(), key = lambda x: x[1]['point'],reverse=True)
print(d_sorted)