根据我在本论坛收到的建议,我使用以下代码(示例)来计算字符串。
phrase_words = ['red car', 'no lake', 'newjersey turnpike']
lines = ['i have a red car which i drove on newjersey', 'turnpike. when i took exit 39 there was no', 'lake. i drove my car on muddy roads which turned my red', 'car into brown. driving on newjersey turnpike can be confusing.']
text = " ".join(lines)
dict = {phrase: text.count(phrase) for phrase in phrase_words}
期望的输出和示例代码的输出是:
{'newjersey turnpike': 2, 'red car': 2, 'no lake': 1}
此代码在小于 300MB 的文本文件上运行良好。我使用了一个 500MB + 大小的文本文件并收到以下内存错误:
y=' '.join(lines)
MemoryError
我该如何克服这个问题?感谢您的帮助!
最佳答案
该算法一次只需要内存中的两行。它假设没有短语会跨越三行:
from itertools import tee, izip
from collections import defaultdict
def pairwise(iterable): # recipe from itertools docs
"s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable)
next(b, None)
return izip(a, b)
d = defaultdict(int)
phrase_words = ['red car', 'no lake', 'newjersey turnpike']
lines = ['i have a red car which i drove on newjersey',
'turnpike. when i took exit 39 there was no',
'lake. i drove my car on muddy roads which turned my red',
'car into brown. driving on newjersey turnpike can be confusing.']
for line1, line2 in pairwise(lines):
both_lines= ' '.join((line1, line2))
for phrase in phrase_words:
# counts phrases in first line and those that span to the next
d[phrase] += both_lines.count(phrase) - line2.count(phrase)
for phrase in phrase_words:
d[phrase] += line2.count(phrase) # otherwise last line is not searched
关于Python:字符串计数内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7475387/