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

时间:2019-01-17 23:46:24

标签: python dictionary

我试图重新排列嵌套词典的内容,在该处它将检查指定键的值。

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

0 个答案:

没有答案