Based on the suggestions I received in this forum, I am using the following code (example) to count strings.
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}
The desired output and the output of the example code is:
{'newjersey turnpike': 2, 'red car': 2, 'no lake': 1}
This code worked great on a text file which was less than 300MB. I used a text file of size 500MB + and received the following memory error:
y=' '.join(lines)
MemoryError
How do I overcome this? Thanks for your help!
y?lines, why are you joining them all intoy? Why not just look at them individually?