我是靠谱客的博主 缓慢小鸭子,这篇文章主要介绍python中sorted对字典进行排序,现在分享给大家,希望可以做个参考。

a = {
    "errorcode": "1000",
    "listFlightInfo": [
        {
            "airporttax": "2",
            "destcity": "PEK",
            "desttime": "09:20",
            "flightNo": "MU5099",
        },
        {
            "airporttax": "1",
            "destcity": "PEK",
            "desttime": "09:10",
            "flightNo": "MU5099",
        },
        {
            "airporttax": "3",
            "destcity": "PEK",
            "desttime": "10:20",
            "flightNo": "MU5099",
        },
    ]}

b = sorted(a['listFlightInfo'], key=lambda x: x['desttime'])  # 正序
c = sorted(a['listFlightInfo'], key=lambda x: x['desttime'], reverse=True)  # 倒序
a['listFlightInfo'] = b
print(a)
a['listFlightInfo'] = c
print(a)

输出:

{'errorcode': '1000', 'listFlightInfo': [{'airporttax': '1', 'destcity': 'PEK', 'desttime': '09:10', 'flightNo': 'MU5099'}, {'airporttax': '2', 'destcity': 'PEK', 'desttime': '09:20', 'flightNo': 'MU5099'}, {'airporttax': '3', 'destcity': 'PEK', 'desttime': '10:20', 'flightNo': 'MU5099'}]}
{'errorcode': '1000', 'listFlightInfo': [{'airporttax': '3', 'destcity': 'PEK', 'desttime': '10:20', 'flightNo': 'MU5099'}, {'airporttax': '2', 'destcity': 'PEK', 'desttime': '09:20', 'flightNo': 'MU5099'}, {'airporttax': '1', 'destcity': 'PEK', 'desttime': '09:10', 'flightNo': 'MU5099'}]}

Process finished with exit code 0

最后

以上就是缓慢小鸭子最近收集整理的关于python中sorted对字典进行排序的全部内容,更多相关python中sorted对字典进行排序内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(115)

评论列表共有 0 条评论

立即
投稿
返回
顶部