python - 如何判断列表中的两个日期在 Python 中是否连续?
问题描述
我已按顺序对字符串日期列表进行了排序
sorteddates =['2017-04-26', '2017-05-05', '2017-05-10', '2017-05-11', '2017-05-16']
我尝试使用它按连续日期对代码进行排序,因为我很难理解。我想看看哪两个日期是连续的。只有两个日期。
dates = [datetime.strptime(d, "%Y-%m-%d") for d in sorteddates]
date_ints = set([d.toordinal() for d in dates])
解决方案
- 将列表从 str 转换为
datetime
-- 仍然按排序顺序。 - 遍历列表;对于每个项目,检查下一个项目是否在一天后 -
datetime
也有timedelta
值。
一些代码:
# Convert list to datetime; you've shown you can do that part.
enter code here
one_day = datetime.timedelta(days=1)
for today, tomorrow in zip(sorteddates, sorteddates[1:]):
if today + one_day == tomorrow:
print ("SUCCESS")
推荐阅读
- php - 如何在 Laravel 中处理用户喜欢另一个用户逻辑?
- jquery - 在 wordpress 开发中使用 jQuery 还可以吗?
- javascript - 为什么我按下按钮时页面会刷新?
- spring - 在配置服务器中解密 git 密码不起作用
- java - 从另一个 JSON 数组中的 JSON 数组中检索多个 JSON 对象?
- csv - bigquery加载一个csv(,字段分隔)文件,在引号之间有数据,并在数据之间得到一列作为逗号
- jquery - ng-click 后未显示 ng-repeat 数据的弹出框
- kubernetes - 如何使 kubernetes cronjob 从文件夹中识别 ssh 密钥
- r - knitr to pdf 插入不需要的新页面
- java - TopologyTestDriver 在位置打开存储时出错