如何循环遍历列表,当达到末尾时从开头开始

huangapple go评论66阅读模式
英文:

How to loop through a list with a set amount of positions and start at the beginning when you reach the end

问题

根据标题所说,我需要循环遍历一个包含7个值的列表,但每次循环都需要从开头开始,以便根据我的代码获取的另一个值获取正确的值,而且需要使用纯Python完成。我知道有库可以做到这一点,但我不想使用它们。所以思路是我有一个包含一周各天的列表,无论我得到多少天后的时间,我都需要获取特定的星期几。

英文:

as the title says i need to loop through a list that contains 7 values but i need to start at the beginning each cycle in order to get the right value based off of another value that my code gets, also needs to be done with pure python. I know that there are libraries to do it but i want to not use them. so the idea is that i have a list of the days of the week and whatever value i get for how many days later the time is i need to get the specific day of the week

答案1

得分: 1

如建议中所述,您可以使用取余(%)运算符,用于获取一个数字除以另一个数字的余数。

它特别有用于在索引值超出范围时持续"循环"将增加的索引值从开头(或减少的索引值从末尾)重新返回。

# 一周的日期列表
days = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
# 一周的总天数
n_days = len(days)

# 从特定日期开始(在本例中为星期天)
start_day = "星期天"

# 获取相应的索引
start_index = days.index(start_day)

# 几天后
n = 20

# 获取几天后的星期日的索引:
d_index = (start_index + 20) % n_days

print("开始日期:", start_day)
print(n, "天后:", days[d_index])

<pre>
开始日期: 星期天
20 天后: 星期六
</pre>

但如果每一步都很重要,那么需要使用循环:

days = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
n_days = len(days)

start_day = "星期天"
start_index = days.index(start_day)
n = 20

print("开始日期:", start_day)
print("\n接下来", n, "天:")

# 遍历接下来的 n 天
for i in range(1, n + 1):
    # 获取当前日期对应的索引。
    d_index = (start_index + i) % n_days
    print(i, days[d_index])

<pre>
开始日期: 星期天

接下来 20 天:
1 星期一
2 星期二
3 星期三
4 星期四
5 星期五
6 星期六
7 星期天
8 星期一
9 星期二
10 星期三
11 星期四
12 星期五
13 星期六
14 星期天
15 星期一
16 星期二
17 星期三
18 星期四
19 星期五
20 星期六
</pre>

以函数形式编写

days_of_week = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天']
n_days = len(days_of_week)

# 输出给定天数后的星期几。
def advance_day(start_day, n=1):
    start_index = days_of_week.index(start_day)
    d_index = (start_index + n) % n_days

    return days_of_week[d_index]

# 输出给定日期后的接下来 n 天的列表。
def next_n_days(start_day, n):
    start_index = days_of_week.index(start_day)
    
    next_days = []
    for i in range(1, n + 1):
        d_index = (start_index + i) % n_days
        next_days.append(days_of_week[d_index])

    return next_days

用法:

# 获取星期天后的第 20 天是星期几。
advance_day("星期天", 20)
# 获取星期天后的接下来 20 天(作为列表)。
next_n_days("星期天", 20)
英文:

As suggested by one of the comments, you could use the modulo (%) operator, used to get the remainder of a number divided by another.

It is especially useful to continuously "roll over" an increased index value back from the beginning (or a decreased index back towards the end) when it goes outside the range.

# List of the days of the week
days = [&#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;, &#39;Saturday&#39;, &#39;Sunday&#39;]
# The total number of days
n_days = len(days)

# Start at a certain day (in this case, Sunday)
start_day = &quot;Sunday&quot;

# Get the corresponding index
start_index = days.index(start_day)

# The number of days later
n = 20

# Get the index of the day of the week n days later:
d_index = (start_index + 20) % n_days

print(&quot;Start day:&quot;, start_day)
print(n, &quot;days later:&quot;, days[d_index])

<pre>
Start day: Sunday
20 days later: Saturday
</pre>

But if every step matters, then a loop would be necessary:

days = [&#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;, &#39;Saturday&#39;, &#39;Sunday&#39;]
n_days = len(days)

start_day = &quot;Sunday&quot;
start_index = days.index(start_day)
n = 20

print(&quot;Start day:&quot;, start_day)
print(&quot;\nNext&quot;, n, &quot;days:&quot;)

# Iterate over the next n days
for i in range(1, n + 1):
    # Get the index corresponding to the current day.
    d_index = (start_index + i) % n_days
    print(i, days[d_index])

<pre>
Start day: Sunday

Next 20 days:
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
8 Monday
9 Tuesday
10 Wednesday
11 Thursday
12 Friday
13 Saturday
14 Sunday
15 Monday
16 Tuesday
17 Wednesday
18 Thursday
19 Friday
20 Saturday
</pre>

Written as functions

days_of_week = [&#39;Monday&#39;, &#39;Tuesday&#39;, &#39;Wednesday&#39;, &#39;Thursday&#39;, &#39;Friday&#39;, &#39;Saturday&#39;, &#39;Sunday&#39;]
n_days = len(days_of_week)

# Outputs the day of the week n days after one given.
def advance_day(start_day, n=1):
    start_index = days_of_week.index(start_day)
    d_index = (start_index + n) % n_days

    return days_of_week[d_index]

# Outputs a list of the next n days after the one given.
def next_n_days(start_day, n):
    start_index = days_of_week.index(start_day)
    
    next_days = []
    for i in range(1, n + 1):
        d_index = (start_index + i) % n_days
        next_days.append(days_of_week[d_index])

    return next_days

Usage:

# Get the day of the week 20 days after Sunday.
advance_day(&quot;Sunday&quot;, 20)
# Get the next 20 days after Sunday (as a list).
next_n_days(&quot;Sunday&quot;, 20)

huangapple
  • 本文由 发表于 2023年2月27日 03:50:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75574629.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定