如何在python 3中从for inside for循环中增加迭代器? [英] how to increment the iterator from inside for loop in python 3?

查看:150
本文介绍了如何在python 3中从for inside for循环中增加迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我想在每次执行send命令时增加迭代器。现在它只在执行for循环时增加1。请告知

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 i in range(0,10):
    print(i)
    i +=2
    print("increased i", i)

我运行此代码并生成从0到9.我原以为它会将迭代器增加2。

I ran this code and it produced out from 0 to 9. I was expecting it would increase the iterator by 2.

推荐答案

将迭代器的副本保存为命名对象。然后,如果您愿意,可以跳过。

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

这篇关于如何在python 3中从for inside for循环中增加迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆