我怎样才能获得两天之间的所有日子?

时间:2009-10-16 11:33:53

标签: python algorithm

我需要两天之间的所有工作日。

示例:

Wednesday - Friday  = Wednesday, Thursday, Friday  
        3 - 5       = 3, 4, 5

 Saturday - Tuesday = Saturday, Sunday, Monday, Tuesday
        6 - 2       = 6, 7, 1, 2

我很确定有一个聪明的算法来解决这个问题。我能想到的唯一算法是使用循环或if语句。

必须有一种优雅的方法来解决这个问题。我在工作日使用数字1-7,但0-6也很好。

我能想出的最好的结果:

def between(d1, d2):
     alldays = [0,1,2,3,4,5,6,0,1,2,3,4,5,6]    # or range(7) * 2
     offset = 8 if d1 > d2 else 1
     return alldays[d1:d2 + offset]

between(0, 4)
# [0,1,2,3,4]

between(5,2)
# [5,6,0,1,2]

7 个答案:

答案 0 :(得分:9)

如何(在伪代码中):

weekday[] = {"Mon" .. "Sun"}
for(i = wkday_start; (i % 7) != wkday_end; i = (i+1) % 7)
    printf("%s ", weekday[i]);

它的工作方式类似于循环缓冲区,wkday_start是从(从0开始)开始的索引,wkday_end是结束索引。

希望这有帮助

乔治。

答案 1 :(得分:8)

>>> def weekdays_between(s, e):
...     return [n % 7 for n in range(s, e + (1 if e > s else 8))]
... 
>>> weekdays_between(2, 4)
[2, 3, 4]
>>> weekdays_between(5, 1)
[5, 6, 0, 1]

如果你必须从/转换到实际日期,那就有点复杂了。

>>> days = 'Mon Tue Wed Thu Fri Sat Sun'.split()
>>> days_1 = {d: n for n, d in enumerate(days)}
>>> def weekdays_between(s, e): 
...     s, e = days_1[s], days_1[e]
...     return [days[n % 7] for n in range(s, e + (1 if e > s else 8))]
... 
>>> weekdays_between('Wed', 'Fri')
['Wed', 'Thu', 'Fri']
>>> weekdays_between('Sat', 'Tue')
['Sat', 'Sun', 'Mon', 'Tue']

答案 2 :(得分:2)

excellent answer from Stephan202的基础上,您可以概括圆形切片的概念。

>>> def circular_slice(r, s, e):
... return [r[n % len(r)] for n in range(s, e + (1 if e>s else len(r)+1))]
...
>>> circular_slice(range(0,7), 2, 4)
[2, 3, 4]
>>> circular_slice(range(0,7), 5, 1)
[5, 6, 0, 1]
>>> circular_slice('Mon Tue Wed Thu Fri Sat Sun'.split(), 5, 1)
['Sat', 'Sun', 'Mon', 'Tue']

答案 3 :(得分:1)

提供的解决方案已经回答了问题,但我想提出一些额外的建议。我不知道你在做什么,但也许你想要实际日期呢?

>>> from datetime import timedelta, date
>>> from dateutil.rrule import rrule, DAILY
>>> today = date(2009, 10, 13) # A tuesday
>>> week = today - timedelta(days=6)
>>> list(rrule(DAILY, byweekday=xrange(5), dtstart=week, until=today))
[datetime.datetime(2009, 10, 7, 0, 0),
 datetime.datetime(2009, 10, 8, 0, 0),
 datetime.datetime(2009, 10, 9, 0, 0),
 datetime.datetime(2009, 10, 12, 0, 0),
 datetime.datetime(2009, 10, 13, 0, 0)]

使用优秀的python-dateutil模块。

答案 4 :(得分:1)

使用日历模块作为日期名称列表:

import calendar

def intervening_days(day1, day2):
    weektest = list(calendar.day_name)*2
    d1 = weektest.index(day1)
    d2 = weektest.index(day2,d1+1)
    return weektest[d1:d2+1]

print intervening_days("Monday","Sunday")
print intervening_days("Monday","Tuesday")
print intervening_days("Thursday","Tuesday")
print intervening_days("Monday","Monday")

打印:

['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
['Monday', 'Tuesday']
['Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday']

如果您不希望星期一到星期一返回整整一周,请将d2的更改更改为d2 = weektest.index(day2,d1)

答案 5 :(得分:0)

你问了一个算法,我明白这应该是语言独立的;但是,以下代码使用C#和LINQ表达式:

DayOfWeek start = DayOfWeek.Wednesday;
DayOfWeek end = DayOfWeek.Friday;

IEnumerable<DayOfWeek> interval = 
    Enum.GetValues(typeof(DayOfWeek)).OfType<DayOfWeek>()
        .Where(d => d >= start && d <= end);

Console.WriteLine(
    String.Join(", ", 
        interval.Select(d => d.ToString()).ToArray()));

可能使用任何语言,您应该将值归因于每一天(Sunday=0等等),并查找符合您所需时间间隔的所有值。

答案 6 :(得分:0)

以下代码在星期一 - 星期一返回1。

bool isWeekday(int d) {
    return d >= 1 && d <= 5;
}

int f(int d1, int d2) {
    int res = isWeekday(d1) ? 1 : 0;
    return d1 == d2 ?
           res :
           res + f(d1 % 7 + 1, d2);
}