递归列表函数

2024-07-07 08:00:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个递归Python函数,该函数接受句点列表,并将它们合并到一个干净的时间轴中。它应该扫描列表并应用以下规则:

  • 如果在期间内找到值:将无替换为datetime.date.today日期()

  • 如果一段时间内开始,而在另一段时间内结束:删除它

  • 如果一段时间之前开始,但另一段时间内结束:延长开始日期

  • 如果一段时间之后结束,但在另一段时间内开始:延长结束日期。

  • 如果一段时间之后开始,而在另一段时间之后结束:保留它,这是一个单独的时间段。

  • 如果一段时间在另一段时间之前开始并且在另一段时间之前结束:保留它,这是一个单独的时间段。

举一个输入和所需输出的例子可能要容易得多(假设值是用datetime格式化的)

[I] = [(01/2011, 02/2015), (04/2012, 08/2014), (09/2014, 03/2015), (05/2015, 06/2016)]
[O] = [(01/2011, 03/2015), (05/2015, 06/2016)]  
# Notice how the output has produced a set of minimum length whilst covering all periods.

[I] = [(07/2011, 02/2015), (04/2012, 08/2014), (09/2014, 04/2015), (06/2015, None)]
[O] = [(07/2011, 04/2015), (06/2015, date.today())]
# Also, notice how the output has amended None, so it can compare dates.

感谢@khredos,我写了以下内容,但它仍然没有输出所需的最小字符串:

from datetime import datetime

# Here is an example list of time periods
periods = [('01/2011', '02/2015'), ('04/2012', '08/2014'), ('09/2014', '03/2015'), ('05/2015', '06/2016')]

# this lambda function converts a string of the format you have specified to a 
# datetime object. If the string is None or empty, it uses today's date
cvt = lambda ds: datetime.strptime(ds, '%m/%Y') if ds else datetime.today()

# Now convert your original list to an iterator that contains datetime objects
periods = list(map(lambda s_e : (cvt(s_e[0]), cvt(s_e[1])), periods))

# Next get the start dates into one list and the end dates into another
starts, ends = zip(*periods)

# Finally get the timeline by sorting the two lists
timeline = sorted(starts + ends)

# Output: [datetime.datetime(2011, 1, 1, 0, 0), datetime.datetime(2012, 4, 1, 0, 0), datetime.datetime(2014, 8, 1, 0, 0), datetime.datetime(2014, 9, 1, 0, 0), datetime.datetime(2015, 2, 1, 0, 0), datetime.datetime(2015, 3, 1, 0, 0), datetime.datetime(2015, 5, 1, 0, 0), datetime.datetime(2016, 6, 1, 0, 0)]

Tags: ofthelambda函数none列表todaydatetime
1条回答
网友
1楼 · 发布于 2024-07-07 08:00:45
from datetime import datetime

# Here is an example list of time periods
periods = [('01/2011', '02/2015'), ('04/2012', '08/2014'), ('09/2014', '03/2015'), ('05/2015', '06/2016')]

# this lambda function converts a string of the format you have specified to a 
# datetime object. If the string is None or empty, it uses today's date
cvt = lambda ds: datetime.strptime(ds, '%m/%Y') if ds else datetime.today()

可用的格式是here

# Now convert your original list to an iterator that contains datetime objects
periods = list(map(lambda s_e : (cvt(s_e[0]), cvt(s_e[1])), periods))

# Next get the start dates into one list and the end dates into another
starts, ends = zip(*periods)

# Finally get the timeline by sorting the two lists
timeline = sorted(starts + ends)

输出应类似于

[datetime.datetime(2011, 1, 1, 0, 0), datetime.datetime(2012, 4, 1, 0, 0), datetime.datetime(2014, 8, 1, 0, 0), datetime.datetime(2014, 9, 1, 0, 0), datetime.datetime(2015, 2, 1, 0, 0), datetime.datetime(2015, 3, 1, 0, 0), datetime.datetime(2015, 5, 1, 0, 0), datetime.datetime(2016, 6, 1, 0, 0)]

尝试一下你的任何一个日期列表,你应该观察到同样的行为。你知道吗

HTH公司

相关问题 更多 >