<p>在python中,有没有某种方法可以在for循环中向前移动索引?例如,如果我在遍历一个字符串并找到我要查找的字符,那么在For循环的下一次迭代中,我可以从该位置开始读取吗?在</p>
<p>比如,在读取x之后,一直读到y,然后在下一次迭代中从y后面的位置开始:</p>
<pre><code>for i in range(len(myString)):
if myString[i] == 'x':
j = i + 1
while( myString[j] != '\n' ):
if( myString[j] == 'y' ):
i = j + 1 # start at position following y in string on the next iteration
j = j + 1
</code></pre>
<p>或者我必须使用while循环来实现这一点?在</p>
<p>您可以尝试使用无限循环:</p>
<pre><code>while true:
if i >= len(myString): # Exit condition
break
# do something with myString[i]
# set i as you want
</code></pre>