[英]How can I calculate the time difference between two lists of dates/times using 365 day format in python?
I have two lists of dates and times in the following format: YYYY:DDD:HH:MM:SS:mmm (mmm is miliseconds).我有两个日期和时间列表,格式如下:YYYY:DDD:HH:MM:SS:mmm(mmm 是毫秒)。
I am getting these times from another list using regular expressions.我使用正则表达式从另一个列表中获取这些时间。
for line in start_list
start_time = re.search(r'((\d\d\d\d):(\d*):(\d\d):(\d\d):(\d\d):(\d\d\d))')
for line in end_list
end_time = re.search(r'((\d\d\d\d):(\d*):(\d\d):(\d\d):(\d\d):(\d\d\d))')
I could do a for loop cycling through start_time, and have a counter to keep track of the current line for end_time.我可以通过 start_time 循环循环,并有一个计数器来跟踪 end_time 的当前行。 I'm not really sure the best way to execute it.
我不确定执行它的最佳方法。 I cant seem to figure out how to cycle through each line in each list to calculate the time difference between them.
我似乎无法弄清楚如何循环遍历每个列表中的每一行来计算它们之间的时间差。 Any help would be very greatly appreciated.
任何帮助将不胜感激。
You can convert to datetime objects and then just subtract the first series to the second as eg:您可以转换为日期时间对象,然后将第一个系列减去第二个系列,例如:
import pandas as pd
start_list_pts=[date.replace(":",".") for date in start_list]
end_list_pts=[date.replace(":",".") for date in end_list]
start_datetimes=pd.to_datetime(start_list_pts,format='%Y.%j.%H.%M.%S.%f')
end_datetimes=pd.to_datetime(end_list_pts,format='%Y.%j.%H.%M.%S.%f')
dts=start_datetimes-end_datetimes
Your milliseconds were not a fraction of your seconds that's why I changed the separator to points.您的毫秒数不是您秒数的一小部分,这就是我将分隔符更改为点的原因。 You can of course also only change the last one!
您当然也可以只更改最后一个!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.