如何从python 3中的for循环内部增加迭代器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32280091/
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
how to increment the iterator from inside for loop in python 3?
提问by ilaunchpad
for i in range (0, 81):
output = send command
while True:
last_byte = last_byte - offset
if last_byte > offset:
output = send command
i+
else:
output = send command
i+
break
I want to increase the iterator every time the send command is executed. Right now it only increases by one when the for loop is executed. Please advise
每次执行发送命令时,我都想增加迭代器。现在它只在 for 循环执行时增加 1。请指教
for i in range(0,10):
print(i)
i +=2
print("increased i", i)
I ran this code and it produced out from 0 to 9. I was expecting it would increase the iterator by 2.
我运行了这段代码,结果从 0 到 9。我原以为它会将迭代器增加 2。
回答by Kasramvd
You can't do that inside a for
loop. Because every time that the loop gets restarted it reassign the variable i
(even after you change it inside the loop) and every time you just increase the reassigned variable. If you want to do such thing you better to use a while
loop and increase the throwaway variable manually.
你不能在for
循环内这样做。因为每次循环重新启动时,它都会重新分配变量i
(即使在循环内更改它之后),并且每次只增加重新分配的变量。如果你想做这样的事情,你最好使用while
循环并手动增加一次性变量。
>>> i=0
>>> while i< 10 :
... print(i)
... i +=2
... print("increased i", i)
...
0
('increased i', 2)
2
('increased i', 4)
4
('increased i', 6)
6
('increased i', 8)
8
('increased i', 10)
Beside that, if you want to increase the variable on a period rather than based some particular condition, you can use a proper slicers to slice the iterable in which you're looping over.
除此之外,如果您想在一段时间内增加变量而不是基于某些特定条件,您可以使用适当的切片器来切片您正在循环的可迭代对象。
For instance if you have an iterator you can use itertools.islice()
if you have a list you can simply use steps while indexing (my_list[start:end:step]
).
例如,如果你有一个迭代器,你可以使用它,itertools.islice()
如果你有一个列表,你可以在索引 ( my_list[start:end:step]
) 时简单地使用步骤。
回答by Matt Anderson
Save a copy of the iterator as a named object. Then you can skip ahead if you want to.
将迭代器的副本保存为命名对象。然后,如果您愿意,可以跳过。
>>> myiter = iter(range(0, 10))
>>> for i in myiter:
print(i)
next(myiter, None)
...
0
2
4
6
8
回答by pzp
range()
has an optional third parameter to specify the step. Use that to increment the counter by two. For example:
range()
有一个可选的第三个参数来指定步骤。使用它可以将计数器增加 2。例如:
for i in range(0, 10, 2):
print(i)
print("increased i", i)
The reason that you cannot increment i
like a normal variable is because when the for-loop starts to execute, a list (or a range object in Python 3+) is created, and i
merely represents each value in that object incrementally.
不能i
像普通变量一样递增的原因是,当 for 循环开始执行时,会创建一个列表(或 Python 3+ 中的范围对象),并且i
仅以递增方式表示该对象中的每个值。
回答by Baradwaj Aryasomayajula
I wrote something like this.
我写了这样的东西。
while True:
if l < 5:
print "l in while", l
l += 1
else:
break
Now I am having a full control but the backdrop is that I have to check all the conditions.
现在我可以完全控制,但背景是我必须检查所有条件。
回答by user7422128
@ilaunchpad Sorry I know it's too late to post this but here is what you were looking for
@ilaunchpad 抱歉,我知道发布此内容为时已晚,但这就是您要查找的内容
i=0
for J in range(0,10):
print(i)
i = i + 2
print("increased i", i)
You should not use the same variable throughout in the For statement.
您不应在 For 语句中始终使用相同的变量。
Output
vaibhav@vaibhav-Lenovo-G570:~$ python3 /home/vaibhav/Desktop/Python/pattern_test.py
0
2
4
6
8
10
12
14
16
18
increased i 20