What is the best way to count the number of matches between the list and the string in python??
for example if I have this list:
list = ['one', 'two', 'three']
and this string:
line = "some one long. two phrase three and one again"
I want to get 4 because I have
one 2 times
two 1 time
three 1 time
I try below code based on this question answers and it's worked but I got error if I add many many words (4000 words) to list:
import re
word_list = ['one', 'two', 'three']
line = "some one long. two phrase three and one again"
words_re = re.compile("|".join(word_list))
print(len(words_re.findall(line)))
This is my error:
words_re = re.compile("|".join(word_list))
File "/usr/lib/python2.7/re.py", line 190, in compile