简体   繁体   English

关于双循环和范围的问题

[英]Question about double for loops and ranges

Since I am a beginner to python I was confused as to why j results to this: 0 1 0 1 2 when doing the code below.由于我是 python 的初学者,我很困惑为什么 j 在执行下面的代码时会产生这样的结果:0 1 0 1 2。 From my understanding I thought, i represents 0-3 so wouldn't j represents the numbers 0-3 as well.根据我的理解,我认为,i 代表 0-3,所以 j 不也代表数字 0-3。

for i in range(4):
    for j in range(i):
        print(j)

Your first for loop goes from 0 to 3.您的第一个 for 循环从 0 变为 3。

So i is 0, then 1, then 2, then 3.所以 i 是 0,然后是 1,然后是 2,然后是 3。

J is going to be 0, then 1, then 2, then 3. J 将是 0,然后是 1,然后是 2,然后是 3。

So printing j in range (0) does nothing, because range(0) is nothing.所以在范围 (0) 中打印 j 什么都不做,因为 range(0) 什么都不是。

Then you print j in range(1), which prints 0然后你在 range(1) 中打印 j,它打印 0

Then you print j in range(2) which prints 0, 1然后你打印 j in range(2) 打印 0, 1

Lastly you print j in range (3) which prints 0, 1, 2最后,您在范围 (3) 中打印 j,该范围打印 0, 1, 2

There are 4 iterations of the outer loop, for i set to 0, 1, 2, and 3.外循环有 4 次迭代,因为i设置为 0、1、2 和 3。

The first one does nothing, as range(0) is an empty sequence.第一个什么都不做,因为range(0)是一个空序列。

The second one outputs 0, the only value in range(1) .第二个输出 0,这是range(1)中的唯一值。

The third outputs 0 and 1, the values in range(2) .第三个输出 0 和 1,即range(2)中的值。

The last prints 0, 1, and 2, the values in range(3) .最后打印 0、1 和 2,即range(3)中的值。

The key is that the inner call to range uses the current value of i to produce a range of values that does not include i itself.关键是对range的内部调用使用i的当前值来生成不包括i本身的值范围。

When you use a for loop on range(x) , It will run its code x times, starting with 0 and incrementing.当您在range(x)上使用 for 循环时,它将运行其代码x次,从 0 开始并递增。

for i in range(4) will run 4 times, each with an i value of 0, 1, 2, 3. for i in range(4)将运行 4 次,每次的i值分别为 0、1、2、3。

When i has a value of 0, for j in range(0) will run 0 times, thus nothing is printed.i的值为 0 时, for j in range(0)将运行 0 次,因此不会打印任何内容。

When i has a value of 1, for j in range(1) will run once, and print 0.i的值为 1 时, for j in range(1)将运行一次,并打印 0。

When i has a value of 2, for j in range(2) will run twice, and print 0, then 1.i的值为 2 时, for j in range(2)将运行两次,并打印 0,然后打印 1。

Lastly, when i has a value of 3, for j in range(3) will run 3 times, and print 0, 1, 2.最后,当i的值为 3 时, for j in range(3)将运行 3 次,并打印 0、1、2。

This gives you an output of:这给你一个 output :

0 1 0 1 2 0 1 0 1 2

Debug example:调试示例:

在此处输入图像描述

In this example for loop for i in range(4) will run while i is smaller than stop value which is in this case 4 (The last run of loop is going to be when i is 3).在此示例中 for 循环for i in range(4)将在 i 小于停止值时运行,在这种情况下为 4(循环的最后一次运行将在 i 为 3 时)。 The default starting value for variable in range function is 0. If you pass two arguments to range function, first will be starting value, and second will be stop value (when variable isn't less then stop value, loop isn't going to run anymore). function 范围内变量的默认起始值为 0。如果将两个 arguments 传递给范围 function,第一个将是起始值,第二个将是停止值再跑)。 With every iteration, the starting value will increase by 1. You can change that number by passing third argument to range function.每次迭代,起始值都会增加 1。您可以通过将第三个参数传递给范围 function 来更改该数字。 If start and stop value are same, for example 0 and 0, for loop won't run.如果 start 和 stop 值相同,例如 0 和 0,则 for 循环不会运行。

It's same with nested for loops, like in your example.这与嵌套的 for 循环相同,就像在您的示例中一样。 Hope I made you for loops little bit easier.希望我让你的循环更容易一些。

Note: I am not so good at English.注意:我的英语不太好。

The same way i represents 0-3 ( which is 1 less than the stop arg 4 );同样的方式i表示 0-3 (stop arg 4 小 1 ); j will take values upto the stop param ( i ) -1 . j将取值直到stop参数 ( i ) -1

On the iteration where i == 3, j will only go upto 2 (ie one less than i ).i == 3 的迭代中, j只会 go 最多 2(即比i小一)。

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

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