簡體   English   中英

如何從元素列表中獲取班次的開始日期和結束日期

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

只是我想要的,如果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

預計 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'}

所以你需要像這樣處理這個問題:

  1. 創建一個空list來存儲dicts (字典列表)。
  2. 檢查每個元素與前一個元素(以確定變化)。
  3. 相應地修改字典。

所以代碼可能是這樣的:

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)

結果是這樣的:

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

您可以自定義以上內容以滿足您的需要...

如果我先隨機得到“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)

預計 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM