Iterate through a single iterator from multiple places in a loop

python at bdurham.com python at bdurham.com
Tue Sep 21 19:10:42 EDT 2010


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?

Thank you,
Malcolm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100921/975b9d4f/attachment.html>


More information about the Python-list mailing list