我的问题可能看起来很愚蠢。但由于我是 Python 新手,请帮帮我。
我必须将一行传递给停用词删除功能。它工作正常。但我的问题是函数的返回是附加单词。我希望它如下所示:
line = " I am feeling good , but I cant talk"
让我们"I,but,cant"
成为停用词。
传递给函数后,我的输出应该是"am feeling good , talk"
。我现在得到的是[['am','feeling','good','talk']]
。
帮我。
我的问题可能看起来很愚蠢。但由于我是 Python 新手,请帮帮我。
我必须将一行传递给停用词删除功能。它工作正常。但我的问题是函数的返回是附加单词。我希望它如下所示:
line = " I am feeling good , but I cant talk"
让我们"I,but,cant"
成为停用词。
传递给函数后,我的输出应该是"am feeling good , talk"
。我现在得到的是[['am','feeling','good','talk']]
。
帮我。
要将列表作为字符串获取,您可以执行以下操作:
>>> out = [['am','feeling','good','talk']]
>>> " ".join(out[0])
'am feeling good talk'
>>>
但是,我认为这更符合您的要求:
>>> line = " I am feeling good , but I cant talk"
>>> [word for word in line.split() if word not in ("I", "but", "cant")]
['am', 'feeling', 'good', ',', 'talk']
>>> lst = [word for word in line.split() if word not in ("I", "but", "cant")]
>>> " ".join(lst)
'am feeling good , talk'
>>>
line = " I am feeling good , but I cant talk"
stop_words={'I','but','cant'}
li=[word for word in line.split() if word not in stop_words]
print li
# prints ['am', 'feeling', 'good', ',', 'talk']
print ' '.join(li)
# prints 'am feeling good , talk'
您可以通过使用列表推导来实现这一点:
def my_function(line, stopwords):
return [word for word in line.split() if word not in stopwords]
stopwords = ['i', 'but', 'cant']
line = " I am feeling good , but I cant talk"
my_function(line, stopwords)
这大致等于下面的这段代码:
def my_function(line, stopwords):
result = []
for i in line.split(): #loop through the lines
if i not in stopwords: #Check if the words are included in stopwords
result.append(i)
结果:
['am', 'feeling', 'good,', 'talk']
希望这可以帮助!