从字符串中删除单词列表 [英] Removing list of words from a string

查看:58
本文介绍了从字符串中删除单词列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个停用词列表.我有一个搜索字符串.我想从字符串中删除单词.

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

举个例子:

stopwords=['what','who','is','a','at','is','he']
query='What is hello'

现在代码应该去掉什么"和是".但是,在我的情况下,它去掉了a"和at".我在下面给出了我的代码.我可能做错了什么?

Now the code should strip 'What' and 'is'. However in my case it strips 'a', as well as 'at'. I have given my code below. What could I be doing wrong?

for word in stopwords:
    if word in query:
        print word
        query=query.replace(word,"")

如果输入查询是What is Hello",我得到的输出为:
什么是什么

If the input query is "What is Hello", I get the output as:
wht s llo

为什么会发生这种情况?

Why does this happen?

推荐答案

这是一种方法:

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)

我注意到如果列表中的小写变体,您还想删除一个单词,因此我在条件检查中添加了对 lower() 的调用.

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.

这篇关于从字符串中删除单词列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆