【发布时间】:2020-03-26 00:37:33
【问题描述】:
我有一个包含两个项目的列表,每个项目都是一个文本字符串。我想循环这两个项目,如果它不在一组单词中,则基本上删除一个单词。但是,以下代码将所有单词放在一起,而不是创建了两个单独的项目。我希望我的 updated_list 有两个项目,一个用于正在更新的每个原始项目:
#stopwords is a variable for a set of words that I dont want in my final updated list
updated_list = []
articles = list_of_articles
for article in articles:
for word in article:
if word not in stopwords:
updated_list.append(word)
articles = [['this, 'is', 'a', 'test'], ['what', 'is', 'your', 'name']]
stopwords = {'is', 'a'}
expected output:
updated_list = [['this, 'test'],['what', 'your', 'name']]
current output:
updated_list = ['this, 'test','what', 'your', 'name']
【问题讨论】:
-
请添加输入和对应的预期输出
-
好的,我添加了输入(文章)和停用词以及预期输出