Reputation: 87
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
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
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
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
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
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