繁体   English   中英

字典中存在检查列表项

[英]Check list item is present in Dictionary

我正在尝试扩展Python - 遍历月份日期并打印自定义 output并添加附加功能来检查给定日期范围内的日期是否是国定假日,打印“NH”和相应的日期。

例如:2020-05-04 和 2020-05-14 是国定假日,仅该日期应打印为在此处输入图像描述

import sys
from datetime import date, datetime, timedelta

year = int(sys.argv[1])
month = int(sys.argv[2])
st_dt = int(sys.argv[3])
en_dt = int(sys.argv[4])
public_holiday = sys.argv[5].split(',')

ph = []
for d in public_holiday:
    ph.append(date(year, month, int(d)))

def daterange(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate <= endDate:
        yield currentDate
        currentDate += delta

allDays = {}
_lastDayType = None

for dte in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
    #print(f"sdfdsfsdf:  {dte.weekday()}")
    if dte.weekday() < 5:
         _dayType = 'working'
    else:
        _dayType = 'weekend'

    _weeknum = dte.strftime("%V")
    _key = (_weeknum, _dayType)
    if _key not in allDays:
        allDays[_key] = []
    allDays[_key].append(dte)

for k,v in allDays.items():
    week_end = ''
    gov_hol = ''
    if len(v) == 1:
        first, last = v[0], v[0]
    elif len(v) == 2 and k[1] == 'weekend':
        first, last, week_end = v[0], v[-1], 'YES'
    elif ph in allDays.values():
        gov_hol, first, last = 'NH', v[0], v[0]
    else:
        first, last = v[0], v[-1]


    print(f"{gov_hol}, {first} >> {last} >> {len(v)} >> {week_end}")

输入参数: date.py 2020 5 1 31 "4, 14"

目前, elif ph in allDays.values():不匹配条件。

当前Output

2020-05-01 >> 2020-05-01 >> 1 >>  >> 
2020-05-02 >> 2020-05-03 >> 2 >> YES >> 
2020-05-04 >> 2020-05-08 >> 5 >>  >> 
2020-05-09 >> 2020-05-10 >> 2 >> YES >> 
2020-05-11 >> 2020-05-15 >> 5 >>  >> 
2020-05-16 >> 2020-05-17 >> 2 >> YES >> 
2020-05-18 >> 2020-05-22 >> 5 >>  >> 
2020-05-23 >> 2020-05-24 >> 2 >> YES >> 
2020-05-25 >> 2020-05-29 >> 5 >>  >> 
2020-05-30 >> 2020-05-31 >> 2 >> YES >> 

第 3 列:天数 第 4 列:如果是周末,则打印“YES”

通常,您希望将日期分组的所有内容都需要添加到字典键中:

year, month, st_dt, en_dt, public_holiday = 2020, 5, 1, 31, (4, 14)

ph = []
for d in public_holiday:
    ph.append(date(year, month, int(d)))

def daterange(startDate, endDate, delta=timedelta(days=1)):
    currentDate = startDate
    while currentDate <= endDate:
        yield currentDate
        currentDate += delta

allDays = {}
_lastDayType = None

for dte in daterange(date(year, month, st_dt), date(year, month, en_dt), delta=timedelta(days=1)):
    if dte.weekday() < 5:
         _dayType = 'working'
    else:
        _dayType = 'weekend'

    _hol = dte in ph                   # True or False
    _weeknum = dte.strftime("%V")
    _key = (_weeknum, _dayType, _hol)  # make unique rows!
    if _key not in allDays:
        allDays[_key] = []
    allDays[_key].append(dte)

for k,v in allDays.items():
    week_end = ''
    gov_hol = k[-1]
    if len(v) == 1:
        first, last = v[0], v[0]
    elif len(v) == 2 and k[1] == 'weekend':
        first, last, week_end = v[0], v[-1], 'YES'
    else:
        first, last = v[0], v[-1]

    print(f"{gov_hol}, {first} >> {last} >> {len(v)} >> {week_end}")

Output:

False, 2020-05-01 >> 2020-05-01 >> 1 >> NO
False, 2020-05-02 >> 2020-05-03 >> 2 >> YES
True, 2020-05-04 >> 2020-05-04 >> 1 >> NO
False, 2020-05-05 >> 2020-05-08 >> 4 >> NO
False, 2020-05-09 >> 2020-05-10 >> 2 >> YES
False, 2020-05-11 >> 2020-05-15 >> 4 >> NO
True, 2020-05-14 >> 2020-05-14 >> 1 >> NO
False, 2020-05-16 >> 2020-05-17 >> 2 >> YES
False, 2020-05-18 >> 2020-05-22 >> 5 >> NO
False, 2020-05-23 >> 2020-05-24 >> 2 >> YES
False, 2020-05-25 >> 2020-05-29 >> 5 >> NO
False, 2020-05-30 >> 2020-05-31 >> 2 >> YES

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM