简体   繁体   English

Python,按索引替代列表

[英]Python, alternate list by index

I have a problem about a python system that has a schedule of 7 days, each day is the index of this list.我有一个关于日程为 7 天的 python 系统的问题,每一天都是这个列表的索引。 The problem is that we have a 24 hours schedule, so each week schedule should alternate like in the picture.问题是我们有一个 24 小时的时间表,所以每个星期的时间表应该像图中那样交替。 One of the rule is that the first week on range always finish on the 6 (Sunday), and the end date of the week can finish in any day.规则之一是范围内的第一周总是在 6(星期日)结束,一周的结束日期可以在任何一天结束。

Maybe I'm seeing this in a harder way that it really is.也许我正在以一种更难的方式看待这一点。 I hope some help.我希望一些帮助。 Thanks in advance.提前致谢。

在此处输入图片说明

Check whether the iteration variable is even or odd, and append the appropriate list to the result.检查迭代变量是偶数还是奇数,并将适当的列表附加到结果中。

result = []
for i in range(5):
    if i % 2 == 0: 
        result += list(range(7))
    else:
        result += list(range(1, 7)) + [1]
print(result)

Something like this works:像这样的工作:

days = 7 
weeks = 4
schedule = [(d+ w%2)%days + int((d+w%2)>(days-1)) for w in range(weeks) for d in range(days)]`

Which essentially says that if it's an odd week (w%2 = 1) add one to the normal schedule and don't allow the final day to be equal to 0, which is why I add the int((d+w%2)>(days-1)) term.这实质上是说,如果这是一个奇数周 (w%2 = 1),则在正常计划中添加一个并且不允许最后一天等于 0,这就是我添加 int((d+w%2 )>(days-1)) 术语。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM