Is there a generator version of `string.split()` in Python?
问题 string.split() returns a list instance. Is there a version that returns a generator instead? Are there any reasons against having a generator version? 回答1: It is highly probable that re.finditer uses fairly minimal memory overhead. def split_iter(string): return (x.group(0) for x in re.finditer(r"[A-Za-z']+", string)) Demo: >>> list( split_iter("A programmer's RegEx test.") ) ['A', "programmer's", 'RegEx', 'test'] edit: I have just confirmed that this takes constant memory in python 3.2.1,