Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 根据给定的日期范围获取周一和周日_Python - Fatal编程技术网

Python 根据给定的日期范围获取周一和周日

Python 根据给定的日期范围获取周一和周日,python,Python,是否可以获取所有周一和周日并将其存储在嵌套列表/元组中 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]) 为了澄清预期结果,我想

是否可以获取所有周一和周日并将其存储在嵌套列表/元组中

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

为了澄清预期结果,我想将
结果
([周一、周日],[周一、周日])分组

您可以使用
日历
模块:

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]
输出:

#sun: 
['2010-01-10', '2010-01-17']
#mon: 
['2010-01-04', '2010-01-11']
[['2010-01-04', '2010-01-10'], ['2010-01-11', '2010-01-17']]
[['2010-01-04', '2010-01-10'], 
 ['2010-01-11', '2010-01-17']]
(['2010-01-04', '2010-01-11'], ['2010-01-10', '2010-01-17'])
编辑:如果要查找输入列表中存在的mon sun范围:

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]
输出:

#sun: 
['2010-01-10', '2010-01-17']
#mon: 
['2010-01-04', '2010-01-11']
[['2010-01-04', '2010-01-10'], ['2010-01-11', '2010-01-17']]
[['2010-01-04', '2010-01-10'], 
 ['2010-01-11', '2010-01-17']]
(['2010-01-04', '2010-01-11'], ['2010-01-10', '2010-01-17'])
您可以尝试以下方法:

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']]

IIUC,使用
itertools.groupby
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
输出:

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

您还可以使用一组默认的列表对日期进行分组,然后只输出周一和周日的工作日编号0-6,在本例中为0和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)
输出:

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

这并不能产生预期的答案。