题目
数据文件:https://labfile.oss.aliyuncs.com/courses/21074/records.txt
Idea
直接通过 datetime 模块加载时间字符串进行格式化,然后对时间列表进行排序,最后两两计算时间差。
Code
Python
from datetime import datetime
time_list = []
with open("records.txt", 'r') as fp:
for line in fp.readlines():
line = line.strip()
tiem_item = datetime.strptime(line, "%Y-%m-%d %H:%M:%S")
time_list.append(tiem_item)
time_list.sort()
ans = 0
for i in range(1, len(time_list), 2):
ans += (time_list[i] - time_list[i - 1]).seconds
print(ans)