0
具有这样的字符串12:65:84
并且告知它表示时间h:m:s
和 值可能不正确,例如, 65
分钟,应翻译为1
小时和5
分钟将可能不正确的时间转换为秒
我需要将这些数字减少到总秒数。
天真的解决方案将是:
time_string = '12:65:84'
hours, minutes, seconds = [int(i) for i in time_string.split(':')
total_seconds = hours * 60 * 60 + minutes * 60 + seconds
问题:如何可以做的更好,最好不使用任何进口,也许与map
,reduce
和他们的朋友一些组合?
任何日期时间模块,你将解析的时间字符串,并抛出错误不正确的值。你的算法有效,有什么问题? – Trimax
@Trimax,这里没有问题,只是问题:“如何做得更好?”。也许与'减少'或什么 – micgeronimo
[如何将H:MM:SS时间字符串转换为Python秒]可能重复(http://stackoverflow.com/questions/6402812/how-to-convert-an- hmmss-time-string-to-seconds-in-python) – Delgan