为什么包含简单while循环的for循环只能运行两次?而且差强人意

时间:2019-06-02 22:37:42

标签: python python-3.x

我正在尝试遍历此while循环几次,以收集有关输出正确的行序列(拼写为“ CODER”)所花费的时间的信息。我希望for循环能循环十次。相反,我只有一个循环,然后在0秒内完成了一次故障重复。

我可以用for i in (0, 10)代替while i < 10行,以后再添加i += 1以获得更长的故障输出(它将第一个结论循环到while循环n次而不是2次)。我还尝试将列表和字典移到for循环中,但这没有使循环正常运行。

from random import randint
from time import sleep
import datetime


stats_collection = []
correct_order = [0, 1, 2, 3, 4]  # A list of numbers between 0 and 4
output_order = []
iterations = 0
the_lines = {0: "...###.................#####...####..",
             1: "..#...#.........####...#.......#...#.",
             2: "..#.......###...#...#..##......##....",
             3: "..#...#..#...#..#...#..#.......#.#...",
             4: "...###....###...####...#####...#..#..",}

for i in (0,10):

    a_currentDT = str(datetime.datetime.now())

    while output_order[-5:] != correct_order:
        next = randint(0, 4) # Select between
        output_order.append(next)
        print(the_lines[next])
        iterations += 1
        # sleep(0.125)

    final_iterations = iterations

    b_currentDT = str(datetime.datetime.now())

    print("Finished!")
    print("Iterations: {num}".format(num=final_iterations))
    print(a_currentDT)
    print(b_currentDT)

    stats_collection.append(final_iterations)

print(stats_collection)

我期望的是类似print(stats_collection)的打印列表,例如:[134, 2948, 4001, 598, 910, 1000, 2000, 3003, 10593, 403]或另一组10个数字。但是我只会将第一次运行打印两次,或者如果我将n循环替换为for循环(如while之类的话,则while i < 10次)。

这是我从for i in (0, 10):获得的输出,作为循环在while循环中的机制:

Finished!
Iterations: 8598
2019-06-02 15:21:21.543503
2019-06-02 15:21:21.641444
Finished!
Iterations: 8598
2019-06-02 15:21:21.641444
2019-06-02 15:21:21.641444
[8598, 8598]

我希望代码第二次运行while循环,并获得完全不同的结果,包括总共10个不同的结果,如下所示:

Finished!
Iterations: 8598
2019-06-02 15:21:21.543503
2019-06-02 15:21:21.641444
Finished!
Iterations: 159
2019-06-02 15:21:21.641445
2019-06-02 15:21:21.700555
[8598, 159]

当然有十种不同的结果。

除此杂项错误外,该代码将非常有用。我实际上不明白为什么代码会这样做。

看到第二个循环实际上是瞬间完成的,这可能会很有用。我完全不知道发生了什么。我不能像这样在while循环中托管for循环吗?

4 个答案:

答案 0 :(得分:3)

for i in (0,10):依次为元组i的每个元素设置(0, 10)。具体来说,它意味着:

  • 使用i=0
  • 运行循环主体
  • 使用i=10
  • 运行循环主体
  • 完成。

如果要在0到(但不包括)10的范围内循环,则需要编写:

for i in range(0, 10):

或者只是:

for i in range(10):

答案 1 :(得分:1)

代码中的两个问题:

  1. for i in (0,10)应该是for i in range(0, 10),前一个仅迭代2次,i = 0 and i = 10

  2. 您应将output_order = []iterations = 0移至for-loop,这也是为什么结果会重复的原因,因为output_order不会重置,并且会保持解决方案在第一次迭代之后。

答案 2 :(得分:0)

您的while循环在3125中有1次机会在每次迭代中产生适当的顺序。大约需要2000次迭代才能有50%的机会找到正确的顺序。 5000次迭代将有80%的机会找到它。这应该提供随机的执行时间,但是由于您的代码现在太紧了,它只会一遍又一遍地重复第一个结果。

随行打印行会大大减慢速度。

由于您没有重置样本之间的output_order,因此第二遍将立即找到匹配的结果(反映在时间中),但由于您也未重置该变量,因此将显示以前的迭代次数。

答案 3 :(得分:0)

我已经对您的代码进行了一些修改,因为您似乎在执行不必要的重复操作。根据我对您的规格的了解,您想计算出拼写编码器需要多少次重复。我将代码更改为仅在随机整数与要打印的当前行的索引匹配后才移至下一行。

`from random import randint
 from time import sleep
 import datetime


 stats_collection = []
 correct_order = [0, 1, 2, 3, 4]  # A list of numbers between 0 and 4
 iterations = 0
 the_lines = {0: "...###.................#####...####..",
         1: "..#...#.........####...#.......#...#.",
         2: "..#.......###...#...#..##......##....",
         3: "..#...#..#...#..#...#..#.......#.#...",
         4: "...###....###...####...#####...#..#..",}

for i in range(0,10):
    output_order = [None for i in range(len(correct_order))]

    a_currentDT = str(datetime.datetime.now())
    idx = 0
    while output_order != correct_order:
        next = randint(0, 4) # Select between
        if output_order[idx] == None and idx == next:
            output_order[idx] = next
            idx += 1
            print(the_lines[next])

        iterations += 1

        # sleep(0.125)
    print(output_order)

    final_iterations = iterations


    b_currentDT = str(datetime.datetime.now())

    print("Finished!")
    print("Iterations: {num}".format(num=final_iterations))
    print(a_currentDT)
    print(b_currentDT)

    stats_collection.append(final_iterations)
    iterations = 0

print(stats_collection)`

我还进行了修改,使其与您一开始的内容最为匹配。

`from random import randint
from time import sleep
import datetime


stats_collection = []
correct_order = [0, 1, 2, 3, 4]  # A list of numbers between 0 and 4
iterations = 0
the_lines = {0: "...###.................#####...####..",
             1: "..#...#.........####...#.......#...#.",
             2: "..#.......###...#...#..##......##....",
             3: "..#...#..#...#..#...#..#.......#.#...",
             4: "...###....###...####...#####...#..#..",}

for i in range(0,10):
    output_order = []

    a_currentDT = str(datetime.datetime.now())

    while output_order[-5:] != correct_order:
        next = randint(0, 4) # Select between
        output_order.append(next)
        iterations += 1
        # print(the_lines[next])
        # sleep(0.125)
    # print(output_order[-5:])

    final_iterations = iterations


    b_currentDT = str(datetime.datetime.now())

    print("Finished!")
    print("Iterations: {num}".format(num=final_iterations))
    print(a_currentDT)
    print(b_currentDT)

    stats_collection.append(final_iterations)
    iterations = 0

print(stats_collection)`