简体   繁体   English

(python) 在嵌套循环中,为什么以及如何缩进 print() 影响结果

[英](python) In nested loops, why and how does indenting print() affect the result

Hi was just playing around and saw that these 2 codes result in two different outcomes.你好只是在玩,看到这两个代码导致两种不同的结果。 Obviously the outcomes going to be different but why?显然结果会有所不同,但为什么呢? How do they work differently?它们的工作方式有何不同? Thanks in advance!提前致谢!

1st code第一个代码

number = [5,2,5,2,2]

for x_count in number:

    output = ""

    for count in range(x_count):

        output = output + "x"

    print(output)

result结果

2nd code第二个代码

number = [5,2,5,2,2]

for x_count in number:

    output = ""

    for count in range(x_count):

        output = output + "x"

        print(output)

result结果

The range() function returns a sequence of numbers, starting from 0 and increments by 1, and stops BEFORE a specified number. range() function 返回一个数字序列,从 0 开始并递增 1,并在指定数字之前停止。

You just need to walk down the loops in steps to figure out what's happening:您只需要逐步走下循环即可弄清楚发生了什么:

The First Program第一个程序

You have a list of integers of 5 and 2. with a total of 5 items.您有一个 5 和 2 的整数列表。共有5项。

number = [5,2,5,2,2]

In the first outer loop, the x_count variable holds the value of each item in the list在第一个外部循环中,x_count 变量保存列表中每个项目的值

for x_count in number:

That means:这意味着:

  • first iteration, x_count = 5第一次迭代, x_count = 5
  • second iteration, x_count = 2第二次迭代, x_count = 2
  • third iteration, x_count = 5 ...and so on第三次迭代, x_count = 5 ...依此类推

Next, you're creating a variable output to hold a string of characters:接下来,您将创建一个变量output来保存一个字符串:

output = ""

The inner loop is where it gets tricky:内部循环是棘手的地方:

for count in range(x_count):

the range function would generate a range of values from zero to x_count , remember the value of the x_count variable is dependent on the current iteration of the outer loop.范围 function 将生成从零到x_count的值范围,请记住x_count变量的值取决于外部循环的当前迭代。 so the range function argument would look like this:所以范围 function 参数看起来像这样:

  • first outer loop iteration - range(5)第一次外循环迭代 - range(5)
  • second outer loop iteration - range(2)第二次外循环迭代 - range(2)
  • third outer loop iteration - range(5) ...and so on第三次外循环迭代 - range(5) ...等等

Within the inner loop, you're concatenating the string "x" to the existing value of output .在内部循环中,您将字符串“x”连接到output的现有值。 And ONLY WHEN the inner loop is complete, you print the value of output (because the print statement is outside the inner loop ie within the outer loop context).并且只有在内循环完成时,才打印output的值(因为打印语句位于内循环之外,即在外循环上下文中)。 That means:这意味着:

  • First inner loop iteration, value of output is "xxxxx" because the inner loop counts up to 5 ( range(x_count) where x_count = 5 )第一次内循环迭代, output的值为"xxxxx" ,因为内循环最多计数 5( range(x_count) where x_count = 5
  • Second inner loop iteration, value of output is "xx" because the inner loop counts up to 2 ( range(x_count) where x_count = 2 )第二次内循环迭代, output的值为"xx" ,因为内循环最多计数 2( range(x_count) where x_count = 2

...and so on. ...等等。

Printing the value of range outside the inner loop would only give you 5 strings, because the print statement is within the outer loop and the outer loop only counts up to 5 (length of the number list).在内部循环之外打印 range 的值只会给你 5 个字符串,因为打印语句在外部循环内,而外部循环最多只能计数 5( number列表的长度)。

The Second Program第二个节目

Everything in the first program applies except the context of the print statement.第一个程序中的所有内容都适用,除了 print 语句的上下文。 in this case, the print statement is within the inner loop context which means it only prints values when an iteration happens within the context of the inner loop.在这种情况下, print语句位于内部循环上下文中,这意味着它仅在内部循环的上下文中发生迭代时才打印值。 it will print the value of output in this order:它将按以下顺序打印output的值:

  • On first iteration of the outer loop, x_count = 5 therefore on the first iteration of the inner loop, the print statement will be called 5 times because the loop counts from 0 - 5 ( range(5) ),在外部循环的第一次迭代中, x_count = 5因此在内部循环的第一次迭代中,print 语句将被调用 5 次,因为循环从 0 到 5 ( range(5) ) 计数,
    • for each iteration in the inner loop, concatenate a new "x" string to output .对于内部循环中的每次迭代,将一个新的“x”字符串连接到output

Result:结果:

x
xx
xxx
xxxx
xxxxx
  • On the second iteration of the outer loop, x_count = 2 , therefore on the second iteration of the inner loop, the print statement will execute 2 times because the loop counts from 0 - 2 ( range(2) )在外循环的第二次迭代中, x_count = 2 ,因此在内循环的第二次迭代中, print 语句将执行 2 次,因为循环从 0 - 2 ( range(2) ) 计数
    • output is reset back to an empty string output 重置为空字符串
    • for each iteration concatenate "x" to output对于每次迭代,将“x”连接到output

Result:结果:

x
xx

Repeating this flow for the next 3 items in the list.对列表中的下 3 个项目重复此流程。 you get:你得到:

range(5):范围(5):

x
xx
xxx
xxxx
xxxxx

range(2):范围(2):

x
xx

range(2):范围(2):

x
xx

Putting it all together when both loops are complete, you have this: Result:当两个循环都完成时,将它们放在一起,您将得到: 结果:

x
xx
xxx
xxxx
xxxxx
x
xx
x
xx
xxx
xxxx
xxxxx
x
xx
x
xx

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

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