I have a dictionary containing people and their duration of completing a task. I wish too add their individual times.
office_time_dict = {
"john": ["0:00:30", "0:02:04", "0:00:16"],
"bryan": ["0:00:30", "0:02:04", "0:00:16"],
"mike": ["0:00:30", "0:02:04", "0:00:16"],
}
total_time = 0
for k, v in office_time_dict.items():
office = office_time_dict[k]
for time in office:
timeparts = [int(s) for s in time.split(":")]
total_time += (timeparts[0] * 60 + timeparts[1]) * 60 + timeparts[2]
total_time, sec = divmod(total_time, 60)
hr, min = divmod(total_time, 60)
print("%d:%02d:%02d" % (hr, min, sec))
current output: 0:02:50, 0:02:52, 0:02:52
Confused as to what I am doing wrong since all outputs should be the same for john, bryan, and mike but bryan and mike are off by 2 seconds
Try this:
office_time_dict = {'john': ['0:00:30', '0:02:04','0:00:16'], 'bryan': ['0:00:30',
'0:02:04','0:00:16'], 'mike' : ['0:00:30', '0:02:04','0:00:16']}
for person in office_time_dict:
office = office_time_dict[person]
total_time = 0 # re-initialise total_time for each person inside loop
for time in office:
timeparts = [int(s) for s in time.split(':')]
total_time += (timeparts[0] * 60 + timeparts[1]) * 60 + timeparts[2]
total_time, sec = divmod(total_time, 60)
hr, min = divmod(total_time, 60)
print('%d:%02d:%02d' % (hr, min, sec))
You'll have a better time if you break out the HH:MM:SS-to-seconds (and vice versa) code to functions of their own:
# Convert a h:m:s string to seconds
def hms_to_sec(hms):
h, m, s = [int(part, 10) for part in hms.split(":")]
return h * 60 * 60 + m * 60 + s
# Convert seconds to h:m:s
def sec_to_hms(s):
hm, s = divmod(s, 60)
h, m = divmod(hm, 60)
return "%02d:%02d:%02d" % (h, m, s)
# Source data (slightly modified from question so the guys have different times)
office_time_dict = {
"john": ["0:00:30", "0:02:04", "0:00:16"],
"bryan": ["0:00:20", "0:03:04", "0:00:16"],
"mike": ["0:00:10", "0:04:04", "0:00:16"],
}
# Convert each person's time to seconds and sum up
total_times_sec = {
person: sum(hms_to_sec(time) for time in times)
for (person, times) in office_time_dict.items()
}
# Convert the summed seconds back to H:M:S
total_times_hms = {
person: sec_to_hms(sec) for (person, sec) in total_times_sec.items()
}
# Sum the seconds and convert back to H:M:S
grand_total_hms = sec_to_hms(sum(total_times_sec.values()))
print(total_times_hms)
print(grand_total_hms)
This prints out
{'john': '00:02:50', 'bryan': '00:03:40', 'mike': '00:04:30'}
00:11:00
You have the variable total_time
initialized only once, before your main loop:
total_time = 0
After every iteration of your main loop the value of total_time
in not 0
, but 2
.
This value is then added in the command
total_time += (timeparts[0] * 60 + timeparts[1]) * 60 + timeparts[2]
How to correct it:
Move the initialization into your main loop:
for k, v in office_time_dict.items():
total_time = 0
You just need to move the initialization of total_time
inside the loop. You can also use the value returned by the items() method instead of access the dictionary by key.
office_time_dict = {'john': ['0:00:30', '0:02:04','0:00:16'],
'bryan': ['0:00:30', '0:02:04','0:00:16'],
'mike' : ['0:00:30', '0:02:04','0:00:16']}
for k, v in office_time_dict.items():
total_time = 0
for time in v:
timeparts = [int(s) for s in time.split(':')]
total_time += (timeparts[0] * 60 + timeparts[1]) * 60 + timeparts[2]
total_time, sec = divmod(total_time, 60)
hr, minutes = divmod(total_time, 60)
print('%d:%02d:%02d' % (hr, minutes, sec))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.