Home > OS >  How get start date & end date for shifts from list of elements
How get start date & end date for shifts from list of elements

Time:12-13

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_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:

{'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'}

CodePudding user response:

So you need to approach the problem like this:

  1. create an empty list to store the dicts (a list of 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...

  • Related