Reputation: 8297
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
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
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
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