首页 > 解决方案 > 根据指定的键值对所有嵌套字典进行排序并返回

问题描述

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

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

在我上面的嵌套字典示例中,我试图通过nameandcreated键(每个 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'
        }
    }
}

标签: pythondictionary

解决方案


推荐阅读