Python 如何使用文本文件从列表中删除停止字

Python 如何使用文本文件从列表中删除停止字,python,editor,Python,Editor,我正在尝试使用包含我自己的停止词的文本文件删除停止词,并尝试创建一个没有停止词的新列表。但是,新列表不会删除停止字 def remove_stopwords(parametera): stopwords = open('myownstopwords.txt') stopwords_list = stopwords.readlines() new_list = [] for parametera in stopwords_list: if parametera not in st

我正在尝试使用包含我自己的停止词的文本文件删除停止词,并尝试创建一个没有停止词的新列表。但是,新列表不会删除停止字

def remove_stopwords(parametera):
 stopwords = open('myownstopwords.txt')
 stopwords_list = stopwords.readlines()
 new_list = []
 for parametera in stopwords_list:
     if parametera not in stop_list:
         new_list.append(parametera)
     stopwords.close()
     new_list.close()
 print(new_list)

有没有办法修理它?我必须列出文本文件中的所有停止字还是可以直接导入它?

以下是接受多个变量的工作代码:

def remove_stopwords(*args):
    with open('myownstopwords.txt','r') as my_stopwords:
        stopwords_list = my_stopwords.read()
        new_list = []
        for arg in args:
            if str(arg) not in stopwords_list:
                new_list.append(arg)
            else:
                pass # You can write something to do if the stopword is found
            my_stopwords.close()
    print(new_list)


remove_stopwords('axe','alien','a')
以下是只有一个变量的代码:

def remove_stopwords(param):
    with open('myownstopwords.txt','r') as my_stopwords:
        stopwords_list = my_stopwords.read()
        new_list = []
        if str(param) not in stopwords_list:
            new_list.append(param)
        else:
            pass # You can write something to do if the stopword is found
        my_stopwords.close()
    print(new_list)


remove_stopwords('axe')
接受列表的代码:

def remove_stopwords(params):
    with open('myownstopwords.txt','r') as my_stopwords:
        stopwords_list = my_stopwords.read()
        new_list = []
        for param in params:
            if str(param) not in stopwords_list:
                new_list.append(param)
            else:
                pass # You can write something to do if the stopword is found
    my_stopwords.close()
    print(new_list)
删除停止字(['axe','a'])

我删除了冗余的
return
语句和
new\u list.close()
因为列表无法关闭,并且摆脱了
for
循环

编辑:对于支持列表,我只是在提供的参数列表上添加了一个for循环

欢迎来到stackoverflow!请在将来写问题时,更清楚地说明您想要实现的目标,并包括所有与您的查询相关的变量和来源


我建议您阅读,以引导您编写一个清晰的问题

为什么要在列表中使用
close
方法<代码>新建列表。关闭()对不起,
停止列表的内容是什么?根据代码,如果
停止列表
中存在单词,则似乎要将
停止列表
中的单词添加到
新列表
中。我没有看到删除…我试图将myownstopwordst.txt中没有的单词添加到新的列表中。因此,新列表中不应包含任何停止字。
return
语句正在破坏您想要执行的操作,它将在第一次循环迭代时停止,下一行没有执行我已经删除了返回,但是它打印出了一个新的列表,里面没有任何内容?嗨,谢谢,但是我尝试了代码它打印出了所有的停止字,但是它也打印了新的列表,停止字仍然在里面?@user10272359是的,我已经更新了我的答案,只是删除了打印(停止字列表)行。我使用它进行测试,但是删除打印(stopwords_列表)只会有助于删除打印列表。当我打印出新列表时,该功能仍然无法正常工作,因为在使用该功能时,停止字仍然会出现?是,它正在按预期工作。但是,def remove_stopwords(*args)中的asterix是什么意思?我尝试更改为另一个参数,但它返回了一个位置参数?@user10272359*args允许您向函数传递数量可变的参数,因此在您的情况下,可以向函数传递多个参数。阅读此链接:。如果有效,请接受以下答案:)