How to sort a list of Python time values?

I have a list of python time values ​​that I extracted from the weblog. I have a list in the format %H:%M:%S How do I sort the time values ​​in ascending order?

+6
source share
4 answers

Just sorted(time_list) works fine.

 >>> sorted(["14:10:01", "03:12:08"]) ["03:12:08", "14:10:01"] 
+5
source
 sorted([tuple(map(int, d.split(":"))) for d in my_time_list]) 

Where each item in my_time_list has the form you describe, for example:

 >>> my_time_list ["03:12:08", "14:10:01"] 
+2
source
 import time sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True) 
+2
source

you should use the sort(key=str.lower) method sort(key=str.lower) , since your time is parsed as a string

0
source

Source: https://habr.com/ru/post/949706/


All Articles