Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing words from list in python

I have a list 'abc' (strings) and I am trying to remove some words present in list 'stop' from the list 'abc' and all the digits present in abc.

abc=[ 'issues in performance 421',
 'how are you doing',
 'hey my name is abc, 143 what is your name',
 'attention pleased',
 'compliance installed 234']
stop=['attention', 'installed']

I am using list comprehension to remove it but this below code is not able to remove that word.

new_word=[word for word in abc if word not in stop ]

Result:(attention word is still present.)

['issues in performance',
 'how are you doing',
 'hey my name is abc, what is your name',
 'attention pleased',
 'compliance installed']

Desired output:

 ['issues in performance',
     'how are you doing',
     'hey my name is abc, what is your name',
     'pleased',
     'compliance']

Thanks

like image 514
user15051990 Avatar asked Sep 23 '26 01:09

user15051990


1 Answers

You need to split each phrase into words and re-join the words into phrases after filtering out those in stop.

[' '.join(w for w in p.split() if w not in stop) for p in abc]

This outputs:

['issues in performance', 'how are you doing', 'hey my name is abc, what is your name', 'pleased', 'compliance installed']
like image 106
blhsing Avatar answered Sep 25 '26 13:09

blhsing