根据4个不同的函数参数生成日期列表

时间:2018-10-21 19:07:24

标签: python datetime

我正在尝试基于4个不同的函数参数生成日期列表,但所有日期应出现在给定时间段(以天为单位)之一的(整数倍)上

import datetime

def daterecurring(star_date,end_date,period1,period2,period3,period4):
    start = datetime.datetime.strptime(star_date, "%d-%m-%Y")
    end = datetime.datetime.strptime(end_date, "%d-%m-%Y")
    date_generated = [start + datetime.timedelta(days=x) for x in range(0,(end-start).days)]
    for date in date_generated:
        print(date.strftime("%d-%m-%Y"))


我已经生成了日期列表,但是我不知道如何使用参数生成。 我想有这样的输出->

Example:    ParamName   Value
Inputs: Start Date  1/1/2018
    End Date    5/1/2018
    Period1 12
    Period2 5
    Period3 10
    Period4 7

Output: Date    Period
    1/6/2018    Period2               # start date + 5 days
    1/8/2018    Period4               # start date + 7 days
    1/11/2018   Period3               # start date + 10 days
    1/11/2018   Period2               # start date + 2*5 days
    1/13/2018   Period1               # start date + 12 days
    1/15/2018   Period4               # start date + 2*7 days
    1/21/2018   Period3               # start date + 2*10 days etc.
    1/23/2018   Period2
    1/25/2018   Period1
    1/27/2018   Period4
    2/2/2018    Period3


预先感谢

1 个答案:

答案 0 :(得分:1)

如果您收集通过添加给函数的每个周期的倍数创建的所有日期,而这些日期仍在给定的日期范围内,则可以将它们收集在字典中。日期是字典中的关键,值是击中该日期的“句号”列表(感谢@vash_the_stampede-我不太明白):

import datetime

def daterecurring(start_date,end_date,p1,p2,p3,p4):
    d = {}
    p_all = p1+p2+p3+p4 
    start = datetime.datetime.strptime(start_date, "%d-%m-%Y")
    end = datetime.datetime.strptime(end_date, "%d-%m-%Y")
    dates = []
    curr_date = start
    days = 0
    for period,num_d in [("Period 1",p1),("Period 2",p2),("Period 3",p3),("Period 4",p4)]:
        curr_date = start
        while curr_date <= end:
            k = d.setdefault(curr_date,[])
            k.append(period)
            curr_date = curr_date + datetime.timedelta(days=num_d)

    # print each date and the list of periods
    for date,ps in sorted(d.items()):
        for period in ps: # one line per period
            print(date.strftime("%d-%m-%Y"), period)

    return dates    

daterecurring("1-1-2018","1-5-2018",12,5,10,7)

将创建以下输出:

01-01-2018 Period 1
01-01-2018 Period 2
01-01-2018 Period 3
01-01-2018 Period 4
06-01-2018 Period 2
08-01-2018 Period 4
11-01-2018 Period 2
11-01-2018 Period 3
13-01-2018 Period 1
15-01-2018 Period 4
16-01-2018 Period 2
21-01-2018 Period 2
21-01-2018 Period 3
22-01-2018 Period 4
25-01-2018 Period 1
26-01-2018 Period 2
29-01-2018 Period 4
31-01-2018 Period 2
31-01-2018 Period 3
05-02-2018 Period 2
05-02-2018 Period 4
06-02-2018 Period 1
10-02-2018 Period 2
10-02-2018 Period 3
12-02-2018 Period 4
15-02-2018 Period 2
18-02-2018 Period 1
19-02-2018 Period 4
20-02-2018 Period 2
20-02-2018 Period 3
25-02-2018 Period 2
26-02-2018 Period 4
02-03-2018 Period 1
02-03-2018 Period 2
02-03-2018 Period 3
05-03-2018 Period 4
07-03-2018 Period 2
12-03-2018 Period 2
12-03-2018 Period 3
12-03-2018 Period 4
14-03-2018 Period 1
17-03-2018 Period 2
19-03-2018 Period 4
22-03-2018 Period 2
22-03-2018 Period 3
26-03-2018 Period 1
26-03-2018 Period 4
27-03-2018 Period 2
01-04-2018 Period 2
01-04-2018 Period 3
02-04-2018 Period 4
06-04-2018 Period 2
07-04-2018 Period 1
09-04-2018 Period 4
11-04-2018 Period 2
11-04-2018 Period 3
16-04-2018 Period 2
16-04-2018 Period 4
19-04-2018 Period 1
21-04-2018 Period 2
21-04-2018 Period 3
23-04-2018 Period 4
26-04-2018 Period 2
30-04-2018 Period 4
01-05-2018 Period 1
01-05-2018 Period 2
01-05-2018 Period 3

关于您提供的excelsheet似乎更合理。