May
May

Reputation: 31

How to filter a sentence based on list of the allowed words in python?

I have allow_wd as words that I want to search.

The sentench is an array of the main database.

The output need:

Newsentench = ['one three','']

Please help

    sentench=['one from twooo or three people are here','he is here']
    allow_wd=['one','two','three','four']

Upvotes: 0

Views: 565

Answers (2)

Reto Weber
Reto Weber

Reputation: 48

Thanks for your clarification, this should be what you want.

sentench=['one from twooo or three people are here','he is here']
allow_wd=['one','two','three','four']

print([" ".join([word for word in s.split(" ") if word in allow_wd]) for s in sentench])

returning: ['one three', '']

Upvotes: 1

Ravindra S
Ravindra S

Reputation: 6432

Using regex boundaries with \b will ensure that two will be strictly matched and won't match twoo.

import re

sentench=['one from twooo or three people are here','he is here']
allow_wd=['one','two','three','four']
newsentench = []

for sent in sentench:
    output = []
    for wd in allow_wd:
        if re.findall('\\b' + wd + '\\b',sent):
            output.append(wd)
    newsentench.append(' '.join(word for word in output))

print(newsentench)

Upvotes: 1

Related Questions