원하는 내용입니다. 1 분 = 60 초이므로 초를 60으로 나눕니다. 문제는 파이썬에서 60.0으로 나누어야 정확한 값 (실수 값)을 얻을 수 있다는 것입니다. 이 값을 얻으면 가장 낮은 정수 값 (math.floor
)을 취합니다. 150 초는 2.5 분이므로 2 분 걸리고 120 초는 2 분, 150에서 30을 제거하십시오. 이것은 남은 초수입니다. sum
이의 합을 계산합니다
total_secs = sum(getSeconds(time_str) for time_str in time_list)
: 당신은 시간에 같은 추론 (60 분)
import sys
import math
def getSeconds(s):
l = s.split(":")
return int(l[0])*(60) + int(l[1])
def getTotalString(time_list):
total_secs = 0
for time_str in time_list:
total_secs += getSeconds(time_str)
total_mins = total_secs/60.0
mins = math.floor(total_mins)
secs = total_secs-mins*60
if mins<60:
return '%.2d:%.2d' % (mins, secs)
else:
hours = math.floor(mins/60.0)
mins = mins-60*hours
return '%.2d:%.2d:%.2d' % (hours, mins, secs)
if __name__ == '__main__':
print getTotalString(sys.argv[1:])
파이썬에 한 줄에서 수행 할 수 초간의 합을 계산하는 방식을 적용 목록 (또는 반복자)의 숫자. 추가 할 숫자 또는 생성자 목록을 제공 할 수 있습니다. the number of seconds in each time string in the list
가능한 한 명확하고 가능한 한 유익하게 작성했으면 좋겠지 만 datetime 모듈의 간단한 솔루션을 사용할 수 있습니다.
업데이트 : 이것은 Python 2.x가 아닌 Python 3입니다. 따라서 예상대로 작동하지 않을 수도 있지만 생각은 동일하지만 구문이 다릅니다.
[Python Time Seconds to h : m : s] 가능한 중복 (http://stackoverflow.com/questions/775049/python-time-seconds-to-hms) –