Reputation: 33
I have a list of for the total time spent logged into a call system.
How can I sum the total time logged in by adding all of the time values together in the list?
The format is hours:minutes:seconds
.
timeList = ['1814:34:54', '205:30:52', '241:59:14', '1310:14:05', '2077:24:12', '1764:44:58', '125:29:49', '1857:19:52', '960:21:27', '1898:35:39', '1680:35:56', '1865:17:39', '34:10:32', '752:55:52', '1887:02:51', '861:54:07', '1927:17:37', '1356:00:31', '51:47:41', '1815:42:44', '1927:37:08', '1375:17:42', '2153:11:28', '805:40:50', '1071:11:56', '1605:12:47', '2032:19:39', '958:59:26', '98:33:42', '731:22:26', '755:02:55', '994:53:57', '264:35:51', '1973:50:07', '1805:00:41', '1554:52:17', '1942:38:34', '1147:16:29', '996:46:31', '1913:41:07', '244:46:29']
Upvotes: 1
Views: 108
Reputation: 2607
Just an alternate way to do it, below answer is very good as well, I suggest you accept it to help out the answerer
from operator import itemgetter
timeList = ['1814:34:54', '205:30:52', ...]
x = ([list(map(int, i.split(":"))) for i in timeList])
x = [sum(map(itemgetter(i), x)) for i in range(3)]
for i in reversed(range(1,3)): y = divmod(x[i], 60); x[i-1] += y[0]; x[i] = y[1]
print(x)
50841:52:34
Upvotes: 0
Reputation: 831
Try this:
timeList = ['1814:34:54', '205:30:52', '241:59:14', '1310:14:05', '2077:24:12', '1764:44:58', '125:29:49', '1857:19:52', '960:21:27', '1898:35:39', '1680:35:56', '1865:17:39', '34:10:32', '752:55:52', '1887:02:51', '861:54:07', '1927:17:37', '1356:00:31', '51:47:41', '1815:42:44', '1927:37:08', '1375:17:42', '2153:11:28', '805:40:50', '1071:11:56', '1605:12:47', '2032:19:39', '958:59:26', '98:33:42', '731:22:26', '755:02:55', '994:53:57', '264:35:51', '1973:50:07', '1805:00:41', '1554:52:17', '1942:38:34', '1147:16:29', '996:46:31', '1913:41:07', '244:46:29']
totalSecs = 0
for tm in timeList:
timeParts = [int(s) for s in tm.split(':')]
totalSecs += (timeParts[0] * 60 + timeParts[1]) * 60 + timeParts[2]
totalSecs, sec = divmod(totalSecs, 60)
hr, min = divmod(totalSecs, 60)
print("%d:%02d:%02d" % (hr, min, sec))
Upvotes: 2