Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

remove a set of words from a list of text

I am trying to remove a list of words from a list of text but output seems like its not getting removed. Please help me in the removing the text from the list

text_list = ['apple is good for health', 'orange and grapes are tasty']
words = ['apple','orange','grapes']
words_format = r'\b(?:{})\b'.format('|',join(words))
remove_words = lambda y: y.replace(words_format,' ')

new_text = list(map(remove_words, text_list))


Expected output:

['is good for health', 'and are tasty']

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I would just split the input, filter out the invalid words and then join the results again:

[" ".join([word for word in text.split(" ") if word not in words]) for text in text_list]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading