Skip to content Skip to sidebar Skip to footer

Python3 Split() With Generator

In Python3 many methods are returning iterator or generator objects (instead of lists or other heavy objects in python2). However, I've found that splitting string still returns li

Solution 1:

Check out re.finditer from the re module => Python Docs

In brief:

""" Returns an iterator yielding match objects over all non-overlapping matches for the RE pattern in string. The string is scanned left-to-right, and matches are returned in the order found. Empty matches are included in the result unless they touch the beginning of another match. """

I think it will do what you need. For example:

import re
text = "This is some nice text"
iter_matches = re.finditer(r'\w+', text)
for match in iter_matches:
    print(match.group(0))

Solution 2:

Regex based answer is small but for those who are still new, and wanted to write one, here is one way to do so:

import string

def gsplit(s,sep=string.whitespace):
    word = []

    for c in s:
        if c in sep:
            if word:
                yield "".join(word)
                word = []
        else:
            word.append(c)

    if word:
        yield "".join(word)
       

text = "This is some nice text"

print(type(gsplit(text)))

for i in (gsplit(text)):
    print(i)
<class 'generator'>
This
is
some
nice
text

[Program finished]

Post a Comment for "Python3 Split() With Generator"