[英]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-01
和end 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:所以你需要像这样处理这个问题:
list
to store the dicts
(a list of dicts).list
来存储dicts
(字典列表)。 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.