Home > OS >  Is there a faster method can checkif words in a sentences list in python?
Is there a faster method can checkif words in a sentences list in python?

Time:09-11

I have a dictionary words list and a sentences list, now I need to check each word(all are different) if in each sentence(all are different) so I my code like that,

word_list = ["人", "天", "地"] #over 100 ords in actual dict
input_file = ["你是不是经常也告诉自己", "不管发生什么事情", "都要微笑着面对生活"] #over 1000 sentences 

output = []
for line in input_file:
    for word in word_list:
        if word in line:
            output.append(word) 

However, I was wondering if it will cost much more time if I use two-loops, is there some better method can finish this job, what about using dict?

CodePudding user response:

To expand my comment:

Given a list of words of length m and a file with n lines with an average line length of l, the following applies.

To turn one line into a set of words, you will have to iterate once over it: O(l).

To turn every line into a set of words will therefore be in O(n * l).

To check if one word is in that set of words, you need to do a set-lookup: O(1).

To check every word will therefore be in O(m).

To do that for every line will therefore be in O(n * m).

  • Related