Thank-you (Re: Iterate through a single iterator from multiple places in a loop)

python at bdurham.com python at bdurham.com
Wed Sep 22 01:46:39 CEST 2010


MRAB,

> it = iter(some_long_string)

<embarrassed> Well, that was easy! :) </embarrassed>

Thanks for your help.

Malcolm


----- Original message -----
From: "MRAB" <python at mrabarnett.plus.com>
To: python-list at python.org
Date: Wed, 22 Sep 2010 00:27:25 +0100
Subject: Re: Iterate through a single iterator from multiple places in a
loop

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
-- 
http://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list