在列表中查找距今天最近的日期 [英] Find the closest date from today in a list

查看:102
本文介绍了在列表中查找距今天最近的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是从列表中获取到今天日期的下一个最接近的日期(将来而不是过去).为简单起见,列表(例如2017-01-31; YYYY-MM-DD的格式)是本赛季中的每场足球比赛,我正在尝试创建一个脚本,以部分找到下一个"足球比赛.

My objective is to get the next closest date (in the future, not past) to today's date from a list. For simplicity's sake, the list (in the format of e.g. 2017-01-31; YYYY-MM-DD) is each football game in the season and I am trying to create a script that in part finds the "next" football game.

我在互联网和堆栈溢出中搜索了答案,并找到了有希望的帖子,但是提供的解决方案使用的是不同的格式,当我尝试将其定制为我的格式时,它会触发异常.

I have searched the internet and Stack Overflow for answers and found a promising post, however the solutions provided are using a different format and when I try to tailor it to mine, it trips exceptions.

我的逻辑包括解析RSS feed,因此我将提供原始列表.考虑到这一点,我的简化代码如下:

My logic includes parsing an RSS feed, so I am just going to provide the raw list instead. With this in mind, my simplified code is as follows:

today = str(datetime.date.today())
print(today)

scheduledatelist = ['2017-09-01', '2017-09-09', '2017-09-16', '2017-09-23', '2017-09-30', '2017-10-07', '2017-10-14', '2017-10-21', '2017-10-27', '2017-11-11', '2017-11-18', '2017-11-25']
scheduledatelist = list(reversed(scheduledatelist)) #purpose: to have earliest dates first

这是我尝试改编上一篇文章的解决方案的尝试(我不精通函数式编程,因此我可能不适合它):

This is my attempt at adapting the previous post's solution (I am not well versed in functional programming, so I may not be adapting it right):

get_datetime = lambda s: datetime.datetime.strptime(s, "%Y-%m-%d")
base = get_datetime(today)
later = filter(lambda d: today(d[0]) > today, scheduledatelist)
closest_date = min(later, key = lambda d: today(d[0]))
print(closest_date)

不管我尝试了什么(这在我的情况下可能不是最好的,因为它更改了格式,并且我需要最终值仍然是YYYY-MM-DD),是否有更简单的方法来做到这一点?我需要下一个(今天最接近)游戏的值,因为它将继续在我的逻辑中使用.因此,回顾一下,我如何才能找到清单中距今天最近的日期,并展望未来.谢谢您的帮助!

Regardless of my attempt (which may not be the best in my situation as it changes the format and I need the end value to still be YYYY-MM-DD), is there an easier way of doing this? I need that next game (closest to today) value as that will continue on to be used in my logic. So to recap, how can I find the closest date in my list, looking toward the future, from today. Thank you for your help!

推荐答案

您可以这样做:

min(scheduledatelist, key=lambda s: 
                  datetime.datetime.strptime(s, "%Y-%m-%d").date()-datetime.date.today())

获取距今天最近的单个日期.

For the single closest date to today.

您可以使用相同的功能按照与今天的距离进行排序:

You can use the same function to sort by distance from today:

sorted(scheduledatelist, key=lambda s: 
              datetime.datetime.strptime(s, "%Y-%m-%d").date()-datetime.date.today())

从今天起,返回的列表将在几天之内越来越远.如果日期在今天之前或之后,则可以使用.

And the returned list will be in increasing distance in days from today. Works if the dates are before or after today.

如果仅希望将来的日期,请过滤掉过去的日期.由于日期字符串采用 ISO 8601 格式,因此可以按词法进行比较:

If you want only dates in the future, filter out the dates in the past. Since the date strings are in ISO 8601 format, you can compare lexically:

min([d for d in scheduledatelist if d>str(datetime.date.today())], key=lambda s: 
              datetime.datetime.strptime(s, "%Y-%m-%d").date()-datetime.date.today())

这篇关于在列表中查找距今天最近的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆