[issue17343] Add a version of str.split which returns an iterator

Juancarlo Añez report at bugs.python.org
Fri Feb 26 10:28:56 EST 2021


Juancarlo Añez <apalala at gmail.com> added the comment:

def isplit(text, sep=None, maxsplit=-1):
    """
    A lowmemory-footprint version of:

        iter(text.split(sep, maxsplit))

    Adapted from https://stackoverflow.com/a/9770397
    """

    if maxsplit == 0:
        yield text
    else:
        rsep = re.escape(sep) if sep else r'\s+'
        regex = fr'(?:^|{rsep})((?:(?!{rsep}).)*)'

        for n, p in enumerate(re.finditer(regex, text)):
            if 0 <= maxsplit <= n:
                yield p.string[p.start(1):]
                return
            yield p.group(1)

----------
nosy: +apalala

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue17343>
_______________________________________


More information about the Python-bugs-list mailing list