根据指定的键值对所有嵌套字典进行排序并返回

2024-10-01 22:39:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试重新排列嵌套字典的内容,它将在其中检查指定键的值

dict_entries = {
    'entries': {
        'AzP746r3Nl': {
            'uniqueID': 'AzP746r3Nl',
            'index': 2,
            'data': {'comment': 'First Plastique Mat.',
                     'created': '17/01/19 10:18',
                     'project': 'EMZ',
                     'name': 'plastique_varA',
                     'version': '1'},
            'name': 'plastique_varA',
            'text': 'plastique test',
            'thumbnail': '/Desktop/mat/plastique_varA/plastique_varA.jpg',
            'type': 'matEntry'
        },  

        'Q2tch2xm6h': {
            'uniqueID': 'Q2tch2xm6h',
            'index': 0,
            'data': {'comment': 'Camino from John Inds.',
                     'created': '03/01/19 12:08',
                     'project': 'EMZ',
                     'name': 'camino_H10a',
                     'version': '1'},
            'name': 'camino_H10a',
            'text': 'John Inds : Camino',
            'thumbnail': '/Desktop/chips/camino_H10a/camino_H10a.jpg',
            'type': 'ChipEntry'
        },

        'ZeqCFCmHqp': {
            'uniqueID': 'ZeqCFCmHqp',
            'index': 1,
            'data': {'comment': 'Prototype Bleu.',
                     'created': '03/01/19 14:07',
                     'project': 'EMZ',
                     'name': 'bleu_P23y',
                     'version': '1'},
            'name': 'bleu_P23y',
            'text': 'Bleu : Prototype',
            'thumbnail': '/Desktop/chips/bleu_P23y/bleu_P23y.jpg',
            'type': 'ChipEntry'
        }
    }
}

在我上面的嵌套字典示例中,我试图通过namecreated键(每个键有2个函数)来检查它,一旦对它进行了排序,index值也会相应地更新

即使如此,我仍然能够查询所述密钥的值:

for item in dict_entries.get('entries').values():
    #The key that I am targetting at
    tar_key = item['name']

但这会返回name键的值,我不确定下一步该怎么做,因为我正在尝试按name键的值排序,并捕获+重新排列嵌套字典的所有内容

这是我想要的输出(如果通过name检查):

{'entries': {
    'ZeqCFCmHqp': {
            'uniqueID': 'ZeqCFCmHqp',
            'index': 1,
            'data': {'comment': 'Prototype Bleu.',
                     'created': '03/01/19 14:07',
                     'project': 'EMZ',
                     'name': 'bleu_P23y',
                     'version': '1'},
            'name': 'bleu_P23y',
            'text': 'Bleu : Prototype',
            'thumbnail': '/Desktop/chips/bleu_P23y/bleu_P23y.jpg',
            'type': 'ChipEntry'
        }

    'Q2tch2xm6h': {
            'uniqueID': 'Q2tch2xm6h',
            'index': 0,
            'data': {'comment': 'Camino from John Inds.',
                     'created': '03/01/19 12:08',
                     'project': 'EMZ',
                     'name': 'camino_H10a',
                     'version': '1'},
            'name': 'camino_H10a',
            'text': 'John Inds : Camino',
            'thumbnail': '/Desktop/chips/camino_H10a/camino_H10a.jpg',
            'type': 'ChipEntry'
        },

    'AzP746r3Nl': {
            'uniqueID': 'AzP746r3Nl',
            'index': 2,
            'data': {'comment': 'First Plastique Mat.',
                     'created': '17/01/19 10:18',
                     'project': 'EMZ',
                     'name': 'plastique_varA',
                     'version': '1'},
            'name': 'plastique_varA',
            'text': 'plastique test',
            'thumbnail': '/Desktop/mat/plastique_varA/plastique_varA.jpg',
            'type': 'matEntry'
        }
    }
}

Tags: nameprojectdataindexversioncommentcreatedbleu

热门问题