Removing list of words from a string

前端 未结 5 2012
一个人的身影
一个人的身影 2020-11-30 06:54

I have a list of stopwords. And I have a search string. I want to remove the words from the string.

As an example:

stopwords=[\'what\',\'who\',\'         


        
5条回答
  •  醉梦人生
    2020-11-30 07:18

    This is one way to do it:

    query = 'What is hello'
    stopwords = ['what', 'who', 'is', 'a', 'at', 'is', 'he']
    querywords = query.split()
    
    resultwords  = [word for word in querywords if word.lower() not in stopwords]
    result = ' '.join(resultwords)
    
    print(result)
    

    I noticed that you want to also remove a word if its lower-case variant is in the list, so I've added a call to lower() in the condition check.

提交回复
热议问题