jd_py
jd_py

Reputation: 87

Get Monday and Sunday based on the given daterange

Is it possible to get all Mondays & Sundays and store it in a nested list / tuple.

Sample data:
dates = [2010-01-01, 2010-01-04, 2010-01-05, 2010-01-06, 2010-01-08, 2010-01-10, 2010-01-11, 2010-01-15, 2010-01-17]

Expected result:
result = ([2010-01-04, 2010-01-10], [2010-01-11, 2010-01-17])

To clarify the expected result, I want to group result with ([Monday, Sunday], [Monday, Sunday]).

Upvotes: 5

Views: 131

Answers (5)

Ajax1234
Ajax1234

Reputation: 71451

You can use the calendar module:

import calendar, datetime
def get_day(d):
   _d = datetime.date(*map(int, d.split('-')))
   c = calendar.Calendar().monthdatescalendar(_d.year, _d.month)
   return [i for k in c for i, a in enumerate(k) if a == _d][0]

dates = ['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06', '2010-01-08', '2010-01-10', '2010-01-11', '2010-01-15', '2010-01-17']
t = [(i, get_day(i)) for i in dates]
sun, mon = [a for a, b in t if b == 6], [a for a, b in t if not b]

Output:

#sun: 
['2010-01-10', '2010-01-17']
#mon: 
['2010-01-04', '2010-01-11']

Edit: if you are looking for mon-sun ranges that exist in the input list:

import calendar, datetime
dates = ['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06', '2010-01-08', '2010-01-10', '2010-01-11', '2010-01-15', '2010-01-17']
d = [datetime.date(*map(int, i.split('-'))) for i in dates]
new_d = [[str(i[0]), str(i[-1])] for j, k in set(map(lambda x:(x.year, x.month), d)) for i in calendar.Calendar().monthdatescalendar(j, k) if i[0] in d and i[-1] in d]

Output:

[['2010-01-04', '2010-01-10'], ['2010-01-11', '2010-01-17']]

Upvotes: 2

Simon Fromme
Simon Fromme

Reputation: 3174

from datetime import datetime

dates = ['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06',
         '2010-01-08', '2010-01-10', '2010-01-11', '2010-01-15',
         '2010-01-17']

def weekday(date_str):
    return datetime.strptime(date_str, '%Y-%m-%d').weekday()

result = [[date for date in dates if weekday(date) == n] for n in {6, 0}]

print(result)
>>> [['2010-01-04', '2010-01-11'], ['2010-01-10', '2010-01-17']]

Upvotes: 4

RoadRunner
RoadRunner

Reputation: 26315

You could also group dates with a defaultdict of lists, then just output the weekday numbers 0-6 for Monday and Sunday, which in this case are 0 and 6.

from datetime import datetime
from collections import defaultdict

dates = [
    "2010-01-01",
    "2010-01-04",
    "2010-01-05",
    "2010-01-06",
    "2010-01-08",
    "2010-01-10",
    "2010-01-11",
    "2010-01-15",
    "2010-01-17",
]

d = defaultdict(list)
for date in dates:
    dt = datetime.strptime(date, "%Y-%m-%d")
    d[dt.weekday()].append(date)

result = (d[0], d[6])
print(result)

Output:

(['2010-01-04', '2010-01-11'], ['2010-01-10', '2010-01-17'])

Upvotes: 1

Chris
Chris

Reputation: 29742

IIUC, using itertools.groupby and datetime.strftime:

from itertools import groupby
from datetime import datetime

dates = [datetime.strptime(x, '%Y-%m-%d') for x in dates]

res = []
for k, g in groupby(dates, key=lambda x: x.strftime('%W')):
    l = [i for i in g if i.weekday() in {0, 6}]
    if l:
        res.append([i.strftime('%Y-%m-%d') for i in l])
res

Output:

[['2010-01-04', '2010-01-10'], 
 ['2010-01-11', '2010-01-17']]

Upvotes: 3

oppressionslayer
oppressionslayer

Reputation: 7204

You can try this:

dates = ['2010-01-01', '2010-01-04', '2010-01-05', '2010-01-06', '2010-01-08', '2010-01-10', '2010-01-11', '2010-01-15', '2010-01-17']
MS=[[], []]
for date in dates:
   dayofweek = datetime.datetime.strptime(date, '%Y-%m-%d').strftime('%A')
   if dayofweek == 'Sunday':
       MS[0].append(date)
   elif dayofweek == 'Monday':
       MS[1].append(date)
MS
#  [['2010-01-10', '2010-01-17'], ['2010-01-04', '2010-01-11']]

Upvotes: 2

Related Questions