def konversi(j=0):
def minute(m=0):
def secon(d=0):
return ((j*60) m)*60 d
return secon
return minute
data = "05:33:05"
data_split = data.split(':')
print("data = ",data)
print("data split = ",data_split)
i'm confuse how to append a value 'day' into fungsion
hours = int(data_split[0])
minutes = int(data_split[1])
second = int(data_split[2])
print("Hours = ", hours)
print("minute = ", minutes)
print("second = ", second)
konvert = konversi(hours)(minutes)(second)
print("Result konversi = ", konvert) #19985
i would like to change data variable from string to list and change the data to like this
data = ["21 day 20 hour 9 minute 20 sec",
"19 day 14 hour 0 minute 13 sec",
"1 day 1 hour 1 minute 1 sec"]
CodePudding user response:
Please help me clarify the question if my answer is not what you want.
I assume this as your input:
data = ["21 day 20 hour 9 minute 20 sec",
"19 day 14 hour 0 minute 13 sec",
"1 day 1 hour 1 minute 1 sec"]
And you want to calculate the total number of seconds for each element in that list.
data_seconds = []
for date_str in data:
day, _, hour, _, minute, _, second, _ = date_str.split(" ")
total_seconds = (int(day) * 24 * 60 * 60
int(hour) * 60 * 60
int(minute) * 60
int(second))
data_seconds.append(total_seconds)
data_seconds:
[1886960, 1692013, 90061]
CodePudding user response:
it might have more sence to store time data as timedelta type. anyway, if you need just total seconds you can get it this way:
from datetime import timedelta
from re import search
pat = r"^(?P<days>\d ) day (?P<hours>\d ) hour (?P<minutes>\d ) minute (?P<seconds>\d ) sec$"
data_dict = lambda x: {k:int(v) for k,v in search(pat, x).groupdict().items()}
res = [timedelta(**data_dict(d)).total_seconds() for d in data] # [1886960.0, 1692013.0, 90061.0]