I am currently looking for a way to find the most recent departure time.
I thought it would be better to convert the current time and timetable into four digits and subtract them, but the following problems have occurred.
I would appreciate it if someone could tell me a good way.
↓ Time data status
arrived_time=['1216', '1337', '1018', '1809', '2058', '1919'...]
time_table=['1210', '1230', '1305', '1328', '1400', '1411'...]
For example, subtracting 1216 to 1210 from 1216 shows a six-minute difference, but subtracting 1130 from 1216 makes it 86 and impossible to calculate.
python
The problem is that the time is calculated in decimal.
You can get the expected results by dividing the units into minutes and subtracting them.
from datetime import datetime as dt
t1 = dt.strptime ('1216', '%H%M')
t2 = dt.strptime ('1130', '%H%M')
d=(t1.hour*60+t1.minute)-(t2.hour*60+t2.minute)
There is a time_table
timetable for a particular station, and you can use the lambda expression to retrieve the most recent value of unsorted time data.
If "last" is literally "time before and after the least difference", it is recommended to take the one with the lowest timedelta
obtained by the difference of datetime
.
If "last" is "last" or "just" then timedelta
is required to determine how many minutes the difference is after all, but the comparison itself can be handled by string comparison by inclusion.
from datetime import datetime
arrived_time=['1216', '1337', '1018', '1809', '2058', '1919', '1159']
time_table=['0000, '1140', '1210', '1230', '1305', '1328', '1411', '1900', '2359']
for at inarrived_time:
dat = datetime.strptime(at, '%H%M')
immediate=min(time_table, key=lambdax:abs(datetime.strptime(x, '%H%M')-dat))#Recent
immediate=max([t for in time_table if <=at])# Immediately before
immediate=min([t for in time_table if > at])# Immediately after
diff = datetime.strptime(immediate, '%H%M') - dat
minutes=abs(diff.total_seconds()/60)
print('{}' most recently '{}', {} minutes difference.'.format(at, immediate, int(minutes)))))
I've commented a lot, but if you're thinking about a transfer guide as @payaneco showed, it's a normal case.
The time difference calculation is introduced in the comments or, as previously answered, use the datetime module (library?).
For example, @akiraejiri-san and @payaneco-san can do the following:
diff=str((dt.strptime (departure time, "%H%M") - dt.strptime (currently "%H%M")).total_seconds()//60))
However, under the conditions presented, it is not necessary to calculate the difference after converting all the data into time, and there is no problem to calculate after finding the most recent departure time (just after).
The time information is a four-digit number, so you can simply sort it up and down the string.
from datetime import datetime as dt
arrived_time=['1216', '1337', '1018', '1809', '2058', '1919']
time_table=['1210', '1230', '1305', '1328', '1400', '1411']
#### combine each time with the attributes of which information
t_arv = [[a,1] for a learned_time]
t_dpt = [[d,2] for in time_table]
t_arv.extend(t_dpt)
#### Sort by time information (because it is a 4-digit number, it can be sorted simply as above and below a string)
t_mix=sorted(t_arv, key=lambdax:x[0])
t_mix_atr=[e[1]for e int_mix]####List only current/departure attributes for search
#### The process of examining the current and most recent departure time pairs and differences
list_result=[ ]
items=len(t_mix)
for i in range (items):
if t_mix[i][1] == 1:##################################################
try:
j=t_mix_atr[i:].index(2)#### Get the index of the most recent departure time after
arv=t_mix[i][0]
dpt=t_mix[i+j][0]
#### Convert string to time, calculate difference value in minutes, and string
diff=str((dt.strptime(dpt, "%H%M")-dt.strptime(arv, "%H%M")).total_seconds()//60))
list_result.append([arv, dpt, diff])#### Add to result list
except:
pass
For in list_result:
print(f'Current: {r[0]}, Departure Time: {r[1]}, Difference: {r[2]} Minutes')
© 2025 OneMinuteCode. All rights reserved.