Hzyf
Hzyf

Reputation: 1017

Searching for strings in a list which contain a specific word

I am writing a code in Python 2.7 in which I have defined a list of more then a million strings. Here is my simple code to find the strings that contain a keyword:

for word in wordlist:
   if keyword in word:
      newlist.append(word)

Is there a better/faster way to accomplish this?

Upvotes: 0

Views: 52

Answers (2)

Hugh Bothwell
Hugh Bothwell

Reputation: 56624

Are you going to be doing this often? Are you only matching exact words? It might be worth building and keeping an index:

from collections import defaultdict

class Index:
    def __init__(self, stringlist):
        self.index = defaultdict(list)
        for string in stringlist:
            for word in string.split():
                self.index[word].append(string)

    def strings_containing(self, word):
        return self.index[word]

Upvotes: 1

thefourtheye
thefourtheye

Reputation: 239433

You can use comprehension like this

newlist = [word for word in wordlist if keyword in word]

Upvotes: 2

Related Questions