返回工作日列表,从给定工作日开始

发布于 2024-09-30 01:01:35 字数 614 浏览 5 评论 0原文

我的任务是定义一个函数 weekdays(weekday),它返回工作日列表,从给定的工作日开始。它应该像这样工作:

>>> weekdays('Wednesday')
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

到目前为止,我已经想出了这个:

def weekdays(weekday):
    days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
            'Sunday')
    result = ""
    for day in days:
        if day == weekday:
            result += day
    return result

但这仅打印输入日期:

>>> weekdays("Sunday")
'Sunday'

我做错了什么?

My task is to define a function weekdays(weekday) that returns a list of weekdays, starting with the given weekday. It should work like this:

>>> weekdays('Wednesday')
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

So far I've come up with this one:

def weekdays(weekday):
    days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
            'Sunday')
    result = ""
    for day in days:
        if day == weekday:
            result += day
    return result

But this prints the input day only:

>>> weekdays("Sunday")
'Sunday'

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

青衫儰鉨ミ守葔 2024-10-07 01:01:36

您可以使用Python标准calendar模块和非常方便的类似列表的deque对象。这样,我们只需将日期列表旋转到我们想要的日期即可。

import calendar
from collections import deque

def get_weekdays(first: str = 'Monday') -> deque[str]:
    weekdays = deque(calendar.day_name)
    weekdays.rotate(-weekdays.index(first))
    return weekdays

get_weekdays('Wednesday')

输出:

deque(['Wednesday',
       'Thursday',
       'Friday',
       'Saturday',
       'Sunday',
       'Monday',
       'Tuesday'])

You can use Python standard calendar module with very convenient list-like deque object. This way, we just have to rotate the list of the days to the one we want.

import calendar
from collections import deque

def get_weekdays(first: str = 'Monday') -> deque[str]:
    weekdays = deque(calendar.day_name)
    weekdays.rotate(-weekdays.index(first))
    return weekdays

get_weekdays('Wednesday')

that outputs:

deque(['Wednesday',
       'Thursday',
       'Friday',
       'Saturday',
       'Sunday',
       'Monday',
       'Tuesday'])
才能让你更想念 2024-10-07 01:01:36

我的简单方法是:

result = days[days.index(weekday):] + days[:days.index(weekdays)]

我希望这有帮助。

My simple approach would be:

result = days[days.index(weekday):] + days[:days.index(weekdays)]

I hope this is helpful.

情徒 2024-10-07 01:01:35

您的代码仅返回一天名称的原因是,weekday 永远不会匹配 days 元组中的多个字符串,因此不会添加任何日期接下来的一周(也不绕回之前的一周)。即使它以某种方式完成,它仍然会将它们作为一个长字符串返回,因为您将 result 初始化为空字符串,而不是空的 list

以下解决方案使用 datetime 模块创建当前区域设置语言中以“Monday”开头的所有工作日名称的列表。然后使用该列表以返回的所需顺序创建另一个名称列表。它通过在原始列表中查找指定日期的索引,然后将相对于该索引的两个片段拼接在一起以形成结果来进行排序。作为一项优化,它还缓存区域设置的日期名称,因此如果使用相同的当前区域设置(可能的情况)再次调用它,则不需要重新创建此私有列表。

import datetime
import locale

def weekdays(weekday):
    current_locale = locale.getlocale()
    if current_locale not in weekdays._days_cache:
        # Add day names from a reference date, Monday 2001-Jan-1 to cache.
        weekdays._days_cache[current_locale] = [
            datetime.date(2001, 1, i).strftime('%A') for i in range(1, 8)]
    days = weekdays._days_cache[current_locale]
    index = days.index(weekday)
    return days[index:] + days[:index]

weekdays._days_cache = {}  # initialize cache

print(weekdays('Wednesday'))
# ['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

除了不需要在函数中硬编码日期名称之外,使用 datetime 模块的另一个优点是使用它的代码将自动以其他语言运行。这可以通过更改区域设置然后使用相应语言的日期名称调用该函数来说明。

例如,虽然法国不是我的默认区域设置,但我可以将其设置为当前区域设置以进行测试,如下所示。注意:根据这篇日期名称的大写文章,法语中星期几的名称不像我的默认英语语言环境中那样大写,但也会自动考虑到这一点,这意味着传递给它的 weekday 名称必须位于当前区域设置的语言,并且区分大小写。当然,如果需要,您可以修改函数以忽略输入参数的字母大小写。

# set or change locale
locale.setlocale(locale.LC_ALL, 'french_france')

print(weekdays('mercredi'))  # use French equivalent of 'Wednesday'
# ['mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'lundi', 'mardi']

The reason your code is only returning one day name is because weekday will never match more than one string in the days tuple and therefore won't add any of the days of the week that follow it (nor wrap around to those before it). Even if it did somehow, it would still return them all as one long string because you're initializing result to an empty string, not an empty list.

Here's a solution that uses the datetime module to create a list of all the weekday names starting with "Monday" in the current locale's language. This list is then used to create another list of names in the desired order which is returned. It does the ordering by finding the index of designated day in the original list and then splicing together two slices of it relative to that index to form the result. As an optimization it also caches the locale's day names so if it's ever called again with the same current locale (a likely scenario), it won't need to recreate this private list.

import datetime
import locale

def weekdays(weekday):
    current_locale = locale.getlocale()
    if current_locale not in weekdays._days_cache:
        # Add day names from a reference date, Monday 2001-Jan-1 to cache.
        weekdays._days_cache[current_locale] = [
            datetime.date(2001, 1, i).strftime('%A') for i in range(1, 8)]
    days = weekdays._days_cache[current_locale]
    index = days.index(weekday)
    return days[index:] + days[:index]

weekdays._days_cache = {}  # initialize cache

print(weekdays('Wednesday'))
# ['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

Besides not needing to hard-code days names in the function, another advantage to using the datetime module is that code utilizing it will automatically work in other languages. This can be illustrated by changing the locale and then calling the function with a day name in the corresponding language.

For example, although France is not my default locale, I can set it to be the current one for testing purposes as shown below. Note: According to this Capitalization of day names article, the names of the days of the week are not capitalized in French like they are in my default English locale, but that is taken into account automatically, too, which means the weekday name passed to it must be in the language of the current locale and is also case-sensitive. Of course you could modify the function to ignore the lettercase of the input argument, if desired.

# set or change locale
locale.setlocale(locale.LC_ALL, 'french_france')

print(weekdays('mercredi'))  # use French equivalent of 'Wednesday'
# ['mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche', 'lundi', 'mardi']
会傲 2024-10-07 01:01:35

一个更快的方法是记住工作日循环。因此,我们只需要获取想要包含在列表中的第一天,并将剩余的 6 个元素添加到末尾。或者换句话说,我们获取从起始日开始的工作日列表,附加另一个完整的一周,并仅返回前 7 个元素(完整的一周)。

days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
def weekdays ( weekday ):
    index = days.index( weekday )
    return list( days[index:] + days )[:7]

>>> weekdays( 'Wednesday' )
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']

A far quicker approach would be to keep in mind, that the weekdays cycle. As such, we just need to get the first day we want to include the list, and add the remaining 6 elements to the end. Or in other words, we get the weekday list starting from the starting day, append another full week, and return only the first 7 elements (for the full week).

days = ('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday')
def weekdays ( weekday ):
    index = days.index( weekday )
    return list( days[index:] + days )[:7]

>>> weekdays( 'Wednesday' )
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
落花浅忆 2024-10-07 01:01:35
def weekdays(day):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    i=days.index(day)   # get the index of the selected day
    d1=days[i:]         # get the list from and including this index
    d1.extend(days[:i]) # append the list from the beginning to this index
    return d1

如果你想测试它是否有效:

def test_weekdays():
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    for day in days:
        print weekdays(day)
def weekdays(day):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    i=days.index(day)   # get the index of the selected day
    d1=days[i:]         # get the list from and including this index
    d1.extend(days[:i]) # append the list from the beginning to this index
    return d1

If you want to test that it works:

def test_weekdays():
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    for day in days:
        print weekdays(day)
红尘作伴 2024-10-07 01:01:35

您不需要对工作日数组进行硬编码。它已在日历模块中提供。

import calendar as cal

def weekdays(weekday):
    start = [d for d in cal.day_name].index(weekday)
    return [cal.day_name[(i+start) % 7] for i in range(7)]

You don't need to hardcode array of weekdays. It's already available in calendar module.

import calendar as cal

def weekdays(weekday):
    start = [d for d in cal.day_name].index(weekday)
    return [cal.day_name[(i+start) % 7] for i in range(7)]
淡淡離愁欲言轉身 2024-10-07 01:01:35

这里还有您想要的更多内容:

def weekdays(weekday):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    index = days.index(weekday)
    return (days + days)[index:index+7]

Here's more what you want:

def weekdays(weekday):
    days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    index = days.index(weekday)
    return (days + days)[index:index+7]
伊面 2024-10-07 01:01:35

嗯,您当前仅搜索给定的工作日并设置为结果:)
您可以使用 python list 中的切片功能来执行此操作:

result = days[days.index(weekday):] + days[:days.index(weekdays)]

Hmm, you are currently only searching for the given weekday and set as result :)
You can use the slice ability in python list to do this:

result = days[days.index(weekday):] + days[:days.index(weekdays)]
如若梦似彩虹 2024-10-07 01:01:35

您的 result 变量是一个 string 而不是 list 对象。此外,它只更新一次,即当它等于传递的工作日参数时。

这是一个实现:

import calendar

def weekdays(weekday):
    days = [day for day in calendar.day_name]
    for day in days:
        days.insert(0, days.pop())    # add last day as new first day of list           
        if days[0] == weekday:        # if new first day same as weekday then all done
            break       
    return days

示例输出:

>>> weekdays("Wednesday")
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
>>> weekdays("Friday")
['Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
>>> weekdays("Tuesday")
['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday']

Your result variable is a string and not a list object. Also, it only gets updated one time which is when it is equal to the passed weekday argument.

Here's an implementation:

import calendar

def weekdays(weekday):
    days = [day for day in calendar.day_name]
    for day in days:
        days.insert(0, days.pop())    # add last day as new first day of list           
        if days[0] == weekday:        # if new first day same as weekday then all done
            break       
    return days

Example output:

>>> weekdays("Wednesday")
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
>>> weekdays("Friday")
['Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday']
>>> weekdays("Tuesday")
['Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday']
往昔成烟 2024-10-07 01:01:35

每次运行 for 循环时,day 变量都会发生变化。所以一天只等于你的输入一次。以“Sunday”为输入,首先检查Monday是否=Sunday,然后检查Tuesday是否=Sunday,然后检查Wednesday=Sunday,直到最后发现Sunday=Sunday并返回Sunday。

Every time you run the for loop, the day variable changes. So day is equal to your input only once. Using "Sunday" as input, it first checked if Monday = Sunday, then if Tuesday = Sunday, then if Wednesday = Sunday, until it finally found that Sunday = Sunday and returned Sunday.

℡Ms空城旧梦 2024-10-07 01:01:35

另一种使用标准库的方法:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
        'Sunday']
def weekdays(weekday):
  n = days.index(weekday)
  return list(itertools.islice(itertools.cycle(days), n, n + 7))

Itertools 在这种情况下有点多。由于您知道最多需要一个额外的周期,因此您可以手动执行此操作:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
        'Sunday']
days += days
def weekdays(weekday):
  n = days.index(weekday)
  return days[n:n+7]

两者都给出预期的输出:

>>> weekdays("Wednesday")
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
>>> weekdays("Sunday")
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
>>> weekdays("Monday")
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']

Another approach using the standard library:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
        'Sunday']
def weekdays(weekday):
  n = days.index(weekday)
  return list(itertools.islice(itertools.cycle(days), n, n + 7))

Itertools is a bit much in this case. Since you know at most one extra cycle is needed, you could do that manually:

days = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday',
        'Sunday']
days += days
def weekdays(weekday):
  n = days.index(weekday)
  return days[n:n+7]

Both give the expected output:

>>> weekdays("Wednesday")
['Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday', 'Monday', 'Tuesday']
>>> weekdays("Sunday")
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
>>> weekdays("Monday")
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
硪扪都還晓 2024-10-07 01:01:35

下面的代码将根据您想要的 X 天生成一个列表,其中您想要生成返回的天数列表,将 [ 减号改为加号 ]

import datetime
numdays = 7
base = datetime.date.today()
date_list = [base + datetime.timedelta(days=x) for x in range(numdays)]
date_list_with_dayname = ["%s, %s" % ((base + datetime.timedelta(days=x)).strftime("%A"),  base + datetime.timedelta(days=x)) for x in range(numdays)]

The code below will gnereate a list based on X days you want a head , of you want to generate list of days going back change the [ minus to plus ]

import datetime
numdays = 7
base = datetime.date.today()
date_list = [base + datetime.timedelta(days=x) for x in range(numdays)]
date_list_with_dayname = ["%s, %s" % ((base + datetime.timedelta(days=x)).strftime("%A"),  base + datetime.timedelta(days=x)) for x in range(numdays)]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文