Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

老铁们,在列表中如何对时间进行排序~

fx  = [{'次数': 2, '时间': '10:45:00'}, {'次数': 4, '时间': '14:58:00'}, {'次数': 5, '时间': '09:59:00'}, {'次数': 28, '时间': '08:13:00'}, {'次数': 34, '时间': '08:28:00'}, {'次数': 4, '时间': '05:12:00'}, {'次数': 12, '时间': '04:56:00'}]

上面的这个列表中,进行时间上的排列,比如最后那个是4点56的,排在第一位,9点59的那个排在最后面。

老铁们,帮帮忙啊~~谢谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
fx.sort(key=lambda item: int(item['时间'][:2])*60 + int(item['时间'][3:5]))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...