Python中是否有`string.split()`的生成器版本? [英] Is there a generator version of `string.split()` in Python?

查看:79
本文介绍了Python中是否有`string.split()`的生成器版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string.split() 返回列表实例.是否有替代版本返回 generator 的版本?是否有任何理由禁止使用生成器版本?

string.split() returns a list instance. Is there a version that returns a generator instead? Are there any reasons against having a generator version?

推荐答案

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))

演示:

>>> list( split_iter("A programmer's RegEx test.") )
['A', "programmer's", 'RegEx', 'test']

我刚刚确认,假设我的测试方法正确,这会在python 3.2.1中占用不变的内存.我创建了一个非常大的字符串(大约1GB),然后使用for循环遍历了可迭代对象(没有列表推导,这会产生额外的内存).这并没有导致内存的显着增长(也就是说,如果存在内存增长,则远远小于1GB的字符串).

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 for loop (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).

这篇关于Python中是否有`string.split()`的生成器版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆