返回工作日列表,从给定工作日开始
我的任务是定义一个函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您可以使用Python标准
calendar
模块和非常方便的类似列表的deque
对象。这样,我们只需将日期列表旋转到我们想要的日期即可。输出:
You can use Python standard
calendar
module with very convenient list-likedeque
object. This way, we just have to rotate the list of the days to the one we want.that outputs:
我的简单方法是:
我希望这有帮助。
My simple approach would be:
I hope this is helpful.
您的代码仅返回一天名称的原因是,
weekday
永远不会匹配days
元组中的多个字符串,因此不会添加任何日期接下来的一周(也不绕回之前的一周)。即使它以某种方式完成,它仍然会将它们作为一个长字符串返回,因为您将result
初始化为空字符串,而不是空的list
。以下解决方案使用
datetime
模块创建当前区域设置语言中以“Monday”开头的所有工作日名称的列表。然后使用该列表以返回的所需顺序创建另一个名称列表。它通过在原始列表中查找指定日期的索引,然后将相对于该索引的两个片段拼接在一起以形成结果来进行排序。作为一项优化,它还缓存区域设置的日期名称,因此如果使用相同的当前区域设置(可能的情况)再次调用它,则不需要重新创建此私有列表。除了不需要在函数中硬编码日期名称之外,使用 datetime 模块的另一个优点是使用它的代码将自动以其他语言运行。这可以通过更改区域设置然后使用相应语言的日期名称调用该函数来说明。
例如,虽然法国不是我的默认区域设置,但我可以将其设置为当前区域设置以进行测试,如下所示。注意:根据这篇日期名称的大写文章,法语中星期几的名称不像我的默认英语语言环境中那样大写,但也会自动考虑到这一点,这意味着传递给它的
weekday
名称必须位于当前区域设置的语言,并且区分大小写。当然,如果需要,您可以修改函数以忽略输入参数的字母大小写。The reason your code is only returning one day name is because
weekday
will never match more than one string in thedays
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 initializingresult
to an empty string, not an emptylist
.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.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.一个更快的方法是记住工作日循环。因此,我们只需要获取想要包含在列表中的第一天,并将剩余的 6 个元素添加到末尾。或者换句话说,我们获取从起始日开始的工作日列表,附加另一个完整的一周,并仅返回前 7 个元素(完整的一周)。
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).
如果你想测试它是否有效:
If you want to test that it works:
您不需要对工作日数组进行硬编码。它已在日历模块中提供。
You don't need to hardcode array of weekdays. It's already available in calendar module.
这里还有您想要的更多内容:
Here's more what you want:
嗯,您当前仅搜索给定的工作日并设置为结果:)
您可以使用 python list 中的切片功能来执行此操作:
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
变量是一个string
而不是list
对象。此外,它只更新一次,即当它等于传递的工作日参数时。这是一个实现:
示例输出:
Your
result
variable is astring
and not alist
object. Also, it only gets updated one time which is when it is equal to the passedweekday
argument.Here's an implementation:
Example output:
每次运行 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.
另一种使用标准库的方法:
Itertools 在这种情况下有点多。由于您知道最多需要一个额外的周期,因此您可以手动执行此操作:
两者都给出预期的输出:
Another approach using the standard library:
Itertools is a bit much in this case. Since you know at most one extra cycle is needed, you could do that manually:
Both give the expected output:
下面的代码将根据您想要的 X 天生成一个列表,其中您想要生成返回的天数列表,将 [ 减号改为加号 ]
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 ]