Python:为什么每个项目都重复此循环?

时间:2018-10-13 11:57:44

标签: python loops iteration

我正在尝试创建一个程序,将飞蛾的每一天与相应的工作日相关联。但是,在附加工作日时遇到了问题。

January  = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
for i in (January):
    for j in days:
        print("January", i,j)

运行时,我得到以下结果:

January 1 Monday
January 1 Tuesday
January 1 Wednesday
January 1 Thursday
January 1 Friday
January 1 Saturday
January 1 Sunday
January 2 Monday
January 2 Tuesday
January 2 Wednesday
January 2 Thursday
January 2 Friday
January 2 Saturday
January 2 Sunday

这种情况一直持续到31岁。

任何帮助将不胜感激,因为我不知道我要去哪里错了。

谢谢! :)

4 个答案:

答案 0 :(得分:2)

在嵌套的for循环中,您说的是“在January中的每一天,在days迭代并打印一些内容。

重复是此处的关键。您将遍历days中每个元素的整个列表January

您实际上想做的是循环访问days的元素,同时循环January的元素。幸运的是,我们为此拥有itertools.cycle

这是我实施您想法的方式。

>>> from itertools import cycle
>>> 
>>> january = range(1, 32)
>>> days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
>>> 
>>> for day, dayname in zip(january, cycle(days)):
...     print('January {} {}'.format(day, dayname))
... 
January 1 Monday
January 2 Tuesday
January 3 Wednesday
January 4 Thursday
January 5 Friday
January 6 Saturday
January 7 Sunday
January 8 Monday
January 9 Tuesday
January 10 Wednesday
January 11 Thursday
January 12 Friday
January 13 Saturday
January 14 Sunday
January 15 Monday
January 16 Tuesday
January 17 Wednesday
January 18 Thursday
January 19 Friday
January 20 Saturday
January 21 Sunday
January 22 Monday
January 23 Tuesday
January 24 Wednesday
January 25 Thursday
January 26 Friday
January 27 Saturday
January 28 Sunday
January 29 Monday
January 30 Tuesday
January 31 Wednesday

如果这不仅仅是编程工作,您可能希望研究datetime模块,以避免手动计算给定日期的工作日。

答案 1 :(得分:1)

第二个循环遍历一周的所有7天,然后再转到该月的第二天。

一种实现所需功能的方法是使用模运算:

January  = ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"]

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

for i in (January):
    # Need to subtract 1 because the loop variable i starts from 1 and not 0 
    current_day = (int(i) % 7) - 1
    print("January", i, days[current_day])

输出:

January 1 Monday
January 2 Tuesday
January 3 Wednesday
January 4 Thursday
January 5 Friday
January 6 Saturday
January 7 Sunday
January 8 Monday
January 9 Tuesday
January 10 Wednesday
January 11 Thursday
January 12 Friday
January 13 Saturday
January 14 Sunday
January 15 Monday
January 16 Tuesday
January 17 Wednesday
January 18 Thursday
January 19 Friday
January 20 Saturday
January 21 Sunday
January 22 Monday
January 23 Tuesday
January 24 Wednesday
January 25 Thursday
January 26 Friday
January 27 Saturday
January 28 Sunday
January 29 Monday
January 30 Tuesday
January 31 Wednesday

答案 2 :(得分:0)

您的代码有几个问题:

  • 您需要循环显示日期字符串(“星期一”,“星期二”等) 反复。您尚未指定任何逻辑来实现此目的。一种解决方案(如下所示)是itertools.cycle
  • 您不需要创建字符串列表。您不需要列表,甚至不需要字符串。只需使用range对象来指定范围。
  • 使用zip依次迭代可迭代的日期数字和字符串。然后,您只需要一个for循环。
  • 您可以将f字符串(Python 3.6+)用作手动字符串连接的高效且易读的替代方法。

这是一个可行的示例:

from itertools import cycle

January_days = range(1, 32)

days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

for day_num, day_str in zip(January_days, cycle(days)):
    print(f'January {day_num} {day_str}')

January 1 Monday
January 2 Tuesday
January 3 Wednesday
January 4 Thursday
...
January 27 Saturday
January 28 Sunday
January 29 Monday
January 31 Wednesday

答案 3 :(得分:0)

for i in (January):
for j in days:
    print("January", i,j)

这是

for i in ["1", "2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"] :
    for j in ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]:
    print("January", i, j)

在外循环的第一次迭代中

i = "1"
j = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

在外循环的第一次迭代中

i = "2"
j = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

....

因此,上面的输出。