简体   繁体   English

如何从元素列表中获取班次的开始日期和结束日期

[英]How get start date & end date for shifts from list of elements

simply what I want if shift == D then I want start date and end date of D, for example in first how many times D appear it 4 times so start date =2021-04-01 and end date = 2021-04-04 that's it same to N also from when N start and end I want just start date, end date and create new dictionary for shift_type leave W but count it also for proper date's I don't understand how do it please check the output that I mentioned只是我想要的,如果shift == D那么我想要 D 的开始日期和结束日期,例如首先 D 出现了 4 次,所以start date =2021-04-01end date = 2021-04-04这对 N 也是一样的,从 N 开始和结束开始我只想开始日期,结束日期并为shift_type创建新字典离开 W 但也计算它的正确日期我不明白怎么做请检查我提到的 output

shift_lst = ["D","D","D","D","N","N","W","N","N","D","D","D","D","W","N","N","N"]
current_date = date(2021, 4, 1)
shift_dict=dict()
for shift in (shift_lst):
    if shift == "D":
        shift_type='S1'
        enddate = current_date + timedelta(days=1)
    elif shift == "W":
        enddate = current_date + timedelta(weeks=1)
    elif shift == "N":
        shift_type='S2'
        enddate = current_date


end_date = current_date
shift_dict['shift_type']=shift_type
shift_dict['start_date']=current_date
shift_dict['end_date']=enddate

Expected Output:预计 Output:

{'shift_type' : 'S1','start_date':'2021,4,1','end_date':'2021,4,4'},
{'shift_type' : 'S2','start_date':'2021,4,5','end_date':'2021,4,9'},
{'shift_type' : 'S1','start_date':'2021,4,11','end_date':'2021,4,13'}

So you need to approach the problem like this:所以你需要像这样处理这个问题:

  1. create an empty list to store the dicts (a list of dicts).创建一个空list来存储dicts (字典列表)。
  2. check each element vs the previous (to identify a change).检查每个元素与前一个元素(以确定变化)。
  3. modify the dicts accordingly.相应地修改字典。

So the code could look like this:所以代码可能是这样的:

import datetime as dt

shift_lst = ["D","D","D","D","N","N","W","N","N","D","D","D","D","W","N","N","N"]
current_date = dt.date(2021, 4, 1)

list_of_shifts = []

# firt element
if shift_lst[0] == 'D': shift_type = 'S1'
if shift_lst[0] == 'N': shift_type = 'S2'

shift_dict = {'shift_type':shift_type, 'start_date':current_date, 'end_date':current_date}

for i in range(1, len(shift_lst)):

    # check if the shift has changed
    if shift_lst[i] != shift_lst[i-1]:
        # and not a weekend
        if shift_lst[i] == "W":
            pass
        else:
            list_of_shifts.append(shift_dict)
            # start a new dict
            shift_dict={'shift_type':shift_type, 'start_date':current_date, 'end_date':current_date}
    
    if shift_lst[i] == 'D':
        shift_type='S1'
        current_date = current_date + dt.timedelta(days=1)
    elif shift_lst[i] == "W":
        current_date = current_date + dt.timedelta(weeks=1) 
    elif shift_lst[i] == "N":
        shift_type='S2'
        current_date = current_date
    shift_dict["end_date"] = current_date


for i in list_of_shifts:
    print(i)

And the result would be this:结果是这样的:

{'shift_type': 'S1', 'start_date': datetime.date(2021, 4, 1), 'end_date': datetime.date(2021, 4, 4)}
{'shift_type': 'S1', 'start_date': datetime.date(2021, 4, 4), 'end_date': datetime.date(2021, 4, 11)}
{'shift_type': 'S2', 'start_date': datetime.date(2021, 4, 11), 'end_date': datetime.date(2021, 4, 11)}
{'shift_type': 'S2', 'start_date': datetime.date(2021, 4, 11), 'end_date': datetime.date(2021, 4, 22)}

You can customise the above to suit your needs...您可以自定义以上内容以满足您的需要...

What if I got "W" first randomly or "L" first (L stands for leave) now see in List shift_lst I got "W" first so leave the "W" or "L" (if L got first) and start from "D" until "N" not come, get start date and end date of "D" like this start_date=2021,4,2 & end_date=2021,4,4 after "D" with "N" when it start on 10th so start_date = 2021,4,10 and end_date = 2021,4,21 so after 2021,4,21 leave "W" and get D Start And end date如果我先随机得到“W”或先得到“L”(L 代表离开)怎么办现在在列表shift_lst中看到我先得到“W”所以留下“W”或“L”(如果 L 得到第一个)并从"D" until "N" not come, get start date and end date of "D" like this start_date=2021,4,2 & end_date=2021,4,4 after "D" with "N" when it starts on 10th所以 start_date = 2021,4,10 和 end_date = 2021,4,21 所以在2021,4,21之后留下“W”并得到 D 开始和结束日期

  • D shift_type = "S1"
  • N shift_type = "S2"
    import datetime as dt
    shift_lst = [ "W","D","D","D","L","L","L","L","L","N","N","N","N","N","N","N","N","N","N","N","N","W","D","D","D","D","D","D","W","D"]
    current_date = dt.date(2021, 4, 1)

    list_of_shifts = []
    shift_type=None

    # firt element
    if shift_lst[0] == 'D': shift_type = 'S1'
    if shift_lst[0] == 'N': shift_type = 'S2'

    for i in range(1, len(shift_lst)):

        # check if the shift has changed
    
        if shift_lst[i] != shift_lst[i - 1]:
        # and not a weekend
        if shift_lst[i] == "W":
            pass
        else:
          
            # start a new dict
            list_of_shifts.append(shift_dict)
            shift_dict = {
                'shift_type': shift_type,
                'start_date': current_date,
                'end_date': current_date
            }
          

        if shift_lst[i] == 'D':
            shift_type = 'S1'
            current_date = current_date + dt.timedelta(days=1)
        elif shift_lst[i] == "W":
            current_date = current_date + dt.timedelta(days=1)
        elif shift_lst[i] == "N":
            shift_type = 'S2'
            current_date = current_date
            current_date = current_date + dt.timedelta(days=1)
        shift_dict["end_date"] = current_date


    for i in list_of_shifts:
        print(i)

Expected output预计 output

{'shift_type': 'S1', 'start_date': datetime.date(2021, 4, 2), 
'end_date': datetime.date(2021, 4, 4)}
{'shift_type': 'S2', 'start_date': datetime.date(2021, 4, 10), 
'end_date': datetime.date(2021, 4, 21)}
{'shift_type': 'S1', 'start_date': datetime.date(2021, 4, 23), 
'end_date': datetime.date(2021, 4, 27)}
{'shift_type': 'S1', 'start_date': datetime.date(2021, 4, 29), 
'end_date': datetime.date(2021, 4, 29)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python。 如何从日期列表中获取开始和结束日期 - Python. How to get start and end date from list of dates 如何使用特定列表从开始日期生成结束日期 - how to generate end date from start date with specific list 从日期和财政年度结束获取季度的开始和结束日期 - Get start and end date of quarter from date and fiscal year end 如何在 PySpark 中获取开始和结束日期? - How to get Start and End date in PySpark? 如何获得给定开始日期和结束日期的中间日期? - How to get the intermediate dates given the start date and end date? 如何从Django中的start_date和end_date获取持续时间? - How to get the duration from start_date and end_date in Django? 如何拆分列表中的元素并从列表中获取日期和时间 - How to split elements in a list and get date and time from a list 如何根据上周的当前日期获取上周作为开始日期和结束日期 - how get the last week as start date and end date based on date in current week in scrapy 如何从 csv 获取日期列表(作为字符串)并仅返回开始日期和结束日期之间的日期/数据? - How can I take list of Dates from csv (as strings) and return only the dates/data between a start date and end date? 起始日期和结束日期之间每周的熊猫元素 - Pandas elements per week between start date and end date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM