Skip to content Skip to sidebar Skip to footer

Check List Item Is Present In Dictionary

I'm trying to extend Python - Iterate thru month dates and print a custom output and add an addtional functionality to check if a date in the given date range is national holiday,

Solution 1:

In general, everything you want the dates to be grouped by needs to be added to the dictionary keys:

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)))

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

allDays = {}
_lastDayType = Nonefor 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 notin allDays:
        allDays[_key] = []
    allDays[_key].append(dte)

for k,v in allDays.items():
    week_end = ''
    gov_hol = k[-1]
    iflen(v) == 1:
        first, last = v[0], v[0]
    eliflen(v) == 2and 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>>NOFalse,2020-05-02>>2020-05-03>>2>>YESTrue,2020-05-04>>2020-05-04>>1>>NOFalse,2020-05-05>>2020-05-08>>4>>NOFalse,2020-05-09>>2020-05-10>>2>>YESFalse,2020-05-11>>2020-05-15>>4>>NOTrue,2020-05-14>>2020-05-14>>1>>NOFalse,2020-05-16>>2020-05-17>>2>>YESFalse,2020-05-18>>2020-05-22>>5>>NOFalse,2020-05-23>>2020-05-24>>2>>YESFalse,2020-05-25>>2020-05-29>>5>>NOFalse,2020-05-30>>2020-05-31>>2>>YES

Post a Comment for "Check List Item Is Present In Dictionary"