从列表中删除常用词



所以,我正在编写一个代码,将交叉检查列表中的每个单词与常用词列表,如果我的列表中的单词=常用词,它将被删除。我试着用我学过的基本术语来解释,以免混淆我的学习。

Alist = ["the", "baby", "is", "so", "happy"]
common = ["the", "so"]
for x in Alist:
for i in common:
if i == Alist[x]:
Alist.remove(common[i])

您可以使用列表推导来过滤单词:

Alist = ["the", "baby", "is", "so", "happy"]
# better convert the common words to set (for faster searching):
common = {"the", "so"}
Alist = [word for word in Alist if word not in common]
print(Alist)

打印:

['baby', 'is', 'happy']

最新更新