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对字典进行排序内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复