按字典嵌套值排序

时间:2015-09-12 00:58:00

标签: python dictionary

我想按照以下字典中的'idx'值进行排序,以创建字典列表

episodes = {
    u '70303660': {
        u 'title': u 'The Forbidden Feast',
        u 'synopsis': u 'Caster and Uryu discuss their views on God, resulting in plans for a horrifying attack -- one that requires everyone else to unite against Caster.',
        u 'summary': {
            u 'notStreaming': False,
            u 'isOriginal': False,
            u 'season': 1,
            u 'idx': 13,
            u '$type': u 'leaf',
            u 'type': u 'episode'
        },
    },
    u '70303654': {
        u 'title': u 'Dark Forest',
        u 'synopsis': u 'The battle is on hold as the group decides what to do about Ryunosuke Uryu and his servant, Caster, who have been kidnapping and sacrificing children.',
        u 'summary': {
            u 'notStreaming': False,
            u 'isOriginal': False,
            u 'season': 1,
            u 'idx': 7,
            u '$type': u 'leaf',
            u 'type': u 'episode'
        },
    },
}

到目前为止,我已经尝试过:

sorted(episodes, lambda x: x['summary']['idx'])

1 个答案:

答案 0 :(得分:3)

您需要遍历子词典,而不是剧集键..

sorted(episodes.values(), key=lambda x: x['summary']['idx'])

(默认情况下,迭代字典会直接遍历其键,而不是其值。)