Dawn17
Dawn17

Reputation: 8297

How can we efficiently check whether a list of string contains a word from another list of strings?

Supposed I have a list of cursewords

curseword = ['fuxx', 'die', 'damn']

and if I am iterating through a list of sentence(list of string) to check if the sentence contains the curse word.

text = [ ['i','am','a','boy'] , [....] , [....] ]

I tried to do something like

for i in curse_words:
    for t in text:
        if i in t:
            // exsits

but it seems wrong and inefficient.

How can I do this efficiently?

Upvotes: 1

Views: 100

Answers (3)

Aaditya Ura
Aaditya Ura

Reputation: 12689

As you said you want something different :

You can try without loop:

curseword = ['fuxx', 'die', 'damn']
text = [ ['i','am','a','damn','boy']]

print(list(filter(lambda z:z!=[None],map(lambda x:(list(map(lambda y:y if x in y else None,text))),curseword))))

output:

[[['i', 'am', 'a', 'damn', 'boy']]]

Upvotes: 0

Ajax1234
Ajax1234

Reputation: 71481

You can cast cursewords to a set for increased lookup efficiency and use list comprehension which is more efficient than more generic looping in smaller cases:

curseword = {'fuxx', 'die', 'damn'}
text = [ ['i','am','a','boy'] , [....] , [....] ]
new_text = map(int, [all(b not in curseword for b in i) for i in text])

Upvotes: 2

Akavall
Akavall

Reputation: 86366

Convert you curseword list to a set, and then user set.intersection to check if words in sentence overlap with cursword.

In [10]: curseword = {'fuxx', 'die', 'damn'}

In [11]: text = [ ['i','am','a','boy'], ['die']]

In [21]: new_text = [int(bool(curseword.intersection(sent))) for sent in text]

In [22]: new_text
Out[22]: [0, 1]

Upvotes: 2

Related Questions