It is highly probable that
re.finditeruses
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, assuming my testing methodology was correct. I created a string of very
large size (1GB or so), then iterated through the iterable with a
forloop
(NOT a list comprehension, which would have generated extra memory). This did
not result in a noticeable growth of memory (that is, if there was a growth in
memory, it was far far less than the 1GB string).
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)