【问题标题】:Don't understand nested For Loop in Python不懂 Python 中的嵌套 For 循环
【发布时间】:2019-03-06 15:26:33
【问题描述】:

我不明白下面代码的第二个 for 循环中的 i 是如何工作的。

di = [96, 15, 33, 87]
for i in range(len(di)):
    total = di[i]
    for j in range(i+1,len(di)):
        print(i)
0
0
0
1
1
2

为什么输出是0,0,0,1,1,2。第二个 for 循环中的 i 如何受到第一个循环的影响?有没有继承?请原谅这里的新手。

【问题讨论】:

  • 尝试使用调试器单步执行程序,并查看每个步骤中ij 的值。
  • 您在j-loop 内打印i - i 不会在其中更改。它打印的次数更少,因为j 循环越短,i 越大。
  • 在这个问题中没有特定于 Python 的内容。这是一个一般(新手)编程问题。尝试打印 (i, j) 看看发生了什么。
  • 在 Python 中,您通常应该避免使用 range 函数。例如,外部 for 循环应该是:for total in di:

标签: python for-loop nested-loops


【解决方案1】:

len(di) 是 4。所以循环

for i in range(len(di)):

将重复 4 次。由于 range 的工作方式(从下限(如果未指定,默认为 0)到上限以下 1),i 在第一次重复中将是 0,在第二次重复中将是 1 , 等等。要计算range(x, y) 生成了多少对象,在这种情况下,for i in range(x, y) 将重复的频率,您可以简单地执行number of repetitions = y - x。所以在这种情况下:len(di) - 0 (default lower bound) = 4

循环

for j in range(i+1, len(di)):
    print(i)

将重复print(i) 命令len(di) - (i + 1) 次。请记住,i 由外部循环定义。所以,在第一个循环中

for i in range(len(di)):

i 等于0,所以print(i) 命令将被执行4 - (0+1) = 3 次——它将打印i(=0) 3 次。在第二个循环中,iequals 1,所以它会被打印 2 次,以此类推。下面是正在发生的事情,将其格式化为代码以提高可读性:

First outer loop: 
i = 0
total = di[i] = di[0] = 96
--> first inner loop of first outer loop:
    j = i + 1 = 1
    i is printed -> prints 0

    second inner loop of first outer loop:
    j = j+1 = 2
    i is printed -> prints 0 again

    third inner loop of first outer loop:
    j = j+1 = 3 --> since len(di) = 4, the upper bound of range(i+1, len(di)) is reached, so this is the last Repetition
    i is printed -> prints 0 again

Second outer loop:
i = 1
total = di[1] = 15
--> first inner loop of second outer loop:
    j = i+1 = 2
    i is printed -> prints 1

    second inner loop of second outer loop:
    j = i+1 = 3  -> upper bound of range reached, last repetition
    i is printed -> prints 1 again

Third outer loop:
i = 2
total = di[2] = 33
--> first inner loop of third outer loop:
    j = i+1 = 3  -> upper bound of range is reached, only Repetition
    i is printed -> prints 2

Fourth (and final) outer loop:
i = 3 -> upper bound of range(len(di)) reached, last Repetition
total = di[3] = 87
since j = i+1 = 4, the inner loop does not get executed at all (both bounds of range are equal), so 3 doesn't get printed

end of code.

【讨论】:

    【解决方案2】:

    在编程语言中,变量可以在范围内使用。当您使用一个新变量开始 for 循环时,它将一直可用,直到您结束它。

    当你开始学习python的旅程时,真正好的做法之一是阅读官方文档。 https://docs.python.org/3/tutorial/controlflow.html

    【讨论】:

    • 本题与range无关
    【解决方案3】:

    为了帮助你理解,试试这个:

    di = [96, 15, 33, 87]
    for i in range(len(di)):
        print("first loop, i =", i)
        total = di[i]
        for j in range(i+1,len(di)):
            print("second loop, j =", j)
            print("second loop, i =", i)
    

    两个循环中的 i 相同。每次外循环运行时,它都会运行内循环,直到“for”语句完成。

    【讨论】:

      猜你喜欢
      • 2012-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2020-10-09
      • 1970-01-01
      • 2020-06-08
      • 2020-05-25
      相关资源
      最近更新 更多