在python中,如何从字符串的arraylist中删除停止字?

2024-04-19 20:00:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从名为arrayList1的arraylist中删除停止字,它存储在数据变量中。 我试过下面的方法,但不管用。请帮助我检查以下代码并改进密码。谢谢. 在

import Retrieve_ED_Notes
from nltk.corpus import stopwords

data = Retrieve_ED_Notes.arrayList1

stop_words = set(stopwords.words('english'))


def remove_stopwords(data):
 data = [word for word in data if word not in stop_words]
 return data

for i in range(0, len(remove_stopwords(data))):
  print(remove_stopwords(data[i]))

arrayList1的控制台输出:

^{pr2}$

Tags: 数据inimportfordataremovewordnotes
1条回答
网友
1楼 · 发布于 2024-04-19 20:00:18

将单词转换为lower并用stopwords检查。在

from nltk.corpus import stopwords
stopwords=set(stopwords.words('english'))

data =['I really love writing journals','The mat is very comfortable and I will buy it again likes','The mousepad is smooth']

def remove_stopwords(data):
    output_array=[]
    for sentence in data:
        temp_list=[]
        for word in sentence.split():
            if word.lower() not in stopwords:
                temp_list.append(word)
        output_array.append(' '.join(temp_list))
    return output_array





output=remove_stopwords(data)

print(output)
['really love writing journals','mat comfortable buy likes', 'mousepad smooth']

相关问题 更多 >