Iterate through a single iterator from multiple places in a loop
MRAB
python at mrabarnett.plus.com
Tue Sep 21 19:27:25 EDT 2010
On 22/09/2010 00:10, python at bdurham.com wrote:
> Is there a pythonic way to loop through a single iterator from
> multiple places in a loop?
>
> Here's pseudo code for what I would like to do:
>
> for char in some_long_string:
> if char == some_char:
> for char in some_long_string: <--- using same iterator as above
> # continue to pull chars from some_long_string
> # until some conditions are met
>
> # repeat above pattern again several times in the loop,
> # with iterator access potentially nested
>
> One solution might be to convert some_long_string into a generator
> that pops of a single character at a time?
>
> Is there a simple way wrap a string as a generator or do I need to
> create a custom function for this?
it = iter(some_long_string)
for char in it:
if char == some_char:
for char in it:
# continue to pull chars from some_long_string
# until some conditions are met
# repeat above pattern again several times in the loop,
# with iterator access potentially nested
More information about the Python-list
mailing list