Python summing up time

后端 未结 9 1697
后悔当初
后悔当初 2020-12-06 00:55

In python how do I sum up the following time?

 0:00:00
 0:00:15
 9:30:56
9条回答
  •  误落风尘
    2020-12-06 01:21

    from datetime import timedelta
    
    h = ['3:00:00','1:07:00', '4:00:00', '4:05:00', '4:10:00', '4:03:00']
    
    def to_td(h):
        ho, mi, se = h.split(':')
        return timedelta(hours=int(ho), minutes=int(mi), seconds=int(se))
    
    print(str(sum(map(to_td, h), timedelta())))
    
    # Out[31]: 20:25:00
    

提交回复
热议问题