How to sort a list of time values?

后端 未结 4 811
再見小時候
再見小時候 2020-12-16 03:47

I have a python list of time values that I extracted from a web log. I have the list in the format of %H:%M:%S. How would I sort the time values in ascending o

相关标签:
4条回答
  • 2020-12-16 03:58
    import time
    
    sorted((time.strptime(d, "%H:%M:%S") for d in time_list), reverse=True)
    
    0 讨论(0)
  • 2020-12-16 03:58
    sorted([tuple(map(int, d.split(":"))) for d in my_time_list])
    

    Where each element in my_time_list is of the form you describe, for example:

    >>> my_time_list
    ["03:12:08", "14:10:01"]
    
    0 讨论(0)
  • 2020-12-16 04:00

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

    0 讨论(0)
  • 2020-12-16 04:06

    Just sorted(time_list) works fine.

    >>> sorted(["14:10:01", "03:12:08"])
    ["03:12:08", "14:10:01"]
    
    0 讨论(0)
提交回复
热议问题