在 Python 中更改 for 循环内的迭代变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15902835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 21:20:14  来源:igfitidea点击:

Changing iteration variable inside for loop in Python

python

提问by nunos

I am trying to do something as simple as changing the varible in which I am iterating over (i) but I am getting different behaviours in both Python and C.

我正在尝试做一些简单的事情,例如更改我在其中迭代 (i) 的变量,但是我在 Python 和 C 中得到了不同的行为。

In Python,

在 Python 中,

for i in range(10):
    print i,
    if i == 2:
        i = 4;

I get 0 1 2 3 4 5 6 7 8 9, but the equivalent in C:

我得到了0 1 2 3 4 5 6 7 8 9,但在 C 中是等价的:

int i;
for (i = 0; i < 10; i++) {
    printf("%d", i);
    if (i == 2)
        i = 4;
}

I get 01256789(note that numbers 3 and 4 don't appear, as expected).

我明白了01256789(请注意,数字 3 和 4 没有出现,正如预期的那样)。

What's happening here?

这里发生了什么事?

采纳答案by Serdalis

You are not doing what you think you are.
For example:

你不是在做你认为的事情。
例如:

for i in range(10):

will constantly set ito be the next element in the range 0-10no matter what.

无论如何,将不断设置i为范围中的下一个元素0-10

If you want to do the equivalent in python you would do:

如果你想在 python 中做等价的,你会这样做:

i = 0
while i < 10:
    print(i)
    if i == 2:
        i = 4
    else:      # these line are
        i += 1 # the correct way
    i += 1 # note that this is wrong if you want 1,2,4,5,6,7,8,9

If you are trying to convert it to Cthen you have to remember that the i++in the forloop will always add to the i.

如果您正试图将其转换为C,那么你必须记住,i++for循环会一直添加到i

回答by sth

Python gives you the elements in range(10), one after another. C repeatedly increments a variable.

Pythonrange(10)一个接一个地为您提供 中的元素。C 重复递增一个变量。

Both of them don't really care what else you do with the variable inside the loop, but since the two language constructs do slightly different things, the outcome is different in some cases.

他们都不太关心你对循环内的变量还做了什么,但是由于两种语言结构做的事情略有不同,因此在某些情况下结果是不同的。

回答by Stephane Rolland

When you call range(10)you create an iteratable list [0,1,2,3,4,5,6,7,8,9].

当您打电话时,range(10)您会创建一个可迭代列表[0,1,2,3,4,5,6,7,8,9]

And the for loop just pick up one number after the other from the list at each turn, whether or not you haved changed the value of i.

并且 for 循环只是在每一轮从列表中一个接一个地选择一个数字,无论您是否更改了i.

回答by Ionut Hulub

The function range()creates a list. For example, range(10)will create the following list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

该函数range()创建一个列表。例如,range(10)将创建以下列表:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

When you say for i in range(10), first off all the list [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]will be generated, and then iwill be assigned all the values from that list, in order.

当您说 时for i in range(10),首先[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]将生成所有列表,然后i按顺序分配该列表中的所有值。

It doesn't matter if you change the value of ibecause on the next iteration it will be assigned the next element from the list.

如果您更改 的值并不重要,i因为在下一次迭代中,它将从列表中分配下一个元素。

It C/C++ on the other hand, at the end of each iteration the value of iis incremented and then compared to the maximum allowed value (in this case 9) and if it is greater, then the loop ends.

另一方面,它是 C/C++,在每次迭代结束时, 的值i递增,然后与最大允许值(在本例中为 9)进行比较,如果它更大,则循环结束。

回答by lvarayut

It's because when you use the range() function in python. Your variable i will be go through the value in range. For example,

这是因为当你在 python 中使用 range() 函数时。您的变量 i 将通过范围内的值。例如,

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

However, the C language that you have written is just using the normally condition to change the value of i. There is no function involved.

但是,您编写的C语言只是使用正常条件来更改i的值。不涉及任何功能。

回答by Pradeep Kanegave

You can not do this by using range function. you have to do it by using while loop only because for loop uses range function and in range function variable will get incremented by its internal method no matter what you specify in the loop it will get incremented by range list only. for i in range(10): ... print i ... if i == 2: ... i = 4 ... else: ... i += 1 ... 0 1 2 3 4 5 6 7 8 9

您不能通过使用范围函数来做到这一点。你必须通过使用 while 循环来做到这一点,因为 for 循环使用范围函数,并且范围函数变量将通过其内部方法递增,无论你在循环中指定什么,它只会通过范围列表递增。for i in range(10): ... 打印 i ... if i == 2: ... i = 4 ... else: ... i += 1 ... 0 1 2 3 4 5 6 7 8 9

An interesting example is here....

一个有趣的例子在这里......

for i in range(10): ... print i ... i = i + 10 ... print i ... this will print... 0 10 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19

for i in range(10): ... 打印 i ... i = i + 10 ... 打印 i ... 这将打印... 0 10 1 11 2 12 3 13 4 14 5 15 6 16 7 17 8 18 9 19