[Tutor] Silly loop question
Kent Johnson
kent37 at tds.net
Tue Aug 16 13:14:19 CEST 2005
mailing list wrote:
> Hi all,
>
> I should know this, but it seems to elude me at present.
>
> I have a loop which is looping through a line of chars by index.
>
> So, one of these -
>
> for i in range(len(line)):
>
>
> Now, question is, is there a simple way to 'fast forward' index i?
>
> At the moment, I'm doing it like this -
>
> changeIndex = None
> al = len(line)
> for i in range(al):
>
> if changeIndex and (i < changeIndex):
> continue
>
> if line[i] == "{":
> nextRightBracket = line.find("}",i)
> #other stuff happens
> changeIndex = nextRightBracket + 1
I would do this with a while loop and an explicit index counter:
al = len(line)
i = 0
while i < al:
if line[i] == "{":
nextRightBracket = line.find("}",i)
#other stuff happens
i = nextRightBracket + 1
else:
i += 1
You could also make a custom iterator that you could set but it doesn't seem worth the trouble. If you just had to skip one or two values I would make an explicit iterator and call next() on it, but to advance an arbitrary amount that is awkward.
Kent
More information about the Tutor
mailing list