How to convert an H:MM:SS time string to seconds in Python?

前端 未结 12 1761
陌清茗
陌清茗 2020-11-29 22:14

Basically I have the inverse of this problem: Python Time Seconds to h:m:s

I have a string in the format H:MM:SS (always 2 digits for minutes and seconds), and I nee

12条回答
  •  悲&欢浪女
    2020-11-29 22:52

    You can use lambda and reduce a list and the fact that m=60s and h=60m. (see "Reducing a List" at http://www.python-course.eu/lambda.php)

    timestamp = "1:23:45"
    seconds = reduce(lambda x, y: x*60+y, [int(i) for i in (timestamp.replace(':',',')).split(',')])
    

提交回复
热议问题