0

我的问题可能看起来很愚蠢。但由于我是 Python 新手,请帮帮我。

我必须将一行传递给停用词删除功能。它工作正常。但我的问题是函数的返回是附加单词。我希望它如下所示:

line = " I am feeling good , but I cant talk"

让我们"I,but,cant"成为停用词。

传递给函数后,我的输出应该是"am feeling good , talk"。我现在得到的是[['am','feeling','good','talk']]

帮我。

4

3 回答 3

0

要将列表作为字符串获取,您可以执行以下操作:

>>> 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'
>>>

这里的重要部分是str.join,str.split列表理解

于 2013-11-06T21:40:06.703 回答
0
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'
于 2013-11-06T21:40:19.120 回答
0

您可以通过使用列表推导来实现这一点:

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']

希望这可以帮助!

于 2013-11-06T21:40:25.717 回答