Python 删除列表中的所有特定单词
我有这样一份清单,Python 删除列表中的所有特定单词,python,Python,我有这样一份清单,[“陆地运输”、“和”、“或”、“港口”、“of”、“测量员”、“和”、“组织机构”]。我想删除所有单词:和,或,的。一、 因此,提出以下代码块 my_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization'] print('Before: {}'.format(my_list)) my_list = list(filter(lambda a: 'and' not
[“陆地运输”、“和”、“或”、“港口”、“of”、“测量员”、“和”、“组织机构”]
。我想删除所有单词:和
,或
,的
。一、 因此,提出以下代码块
my_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
print('Before: {}'.format(my_list))
my_list = list(filter(lambda a: 'and' not in a and 'of' not in a and 'or' not in a, my_list))
print('After: {}'.format(my_list))
但是,我的代码给出如下输出
Before: ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
After: []
我想要的应该是
['land_transport', 'port', 'surveyor', 'organization']
当然,有几种方法可供选择。但是我想坚持使用lambda函数来解决这个问题。对我的问题有什么建议吗?您可以创建一个新列表,存储所有要筛选的单词:
my_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
to_remove = ['or', 'of', 'and']
new_list = list(filter(lambda x:x not in to_remove, my_list))
输出:
['land_transport', 'port', 'surveyor', 'organization']
您可以创建一个新列表,存储所有要筛选的单词:
my_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
to_remove = ['or', 'of', 'and']
new_list = list(filter(lambda x:x not in to_remove, my_list))
输出:
['land_transport', 'port', 'surveyor', 'organization']
您的筛选不正确。请使用:
filter_set = {'and', 'or', 'of'}
my_list = list(filter(lambda a: a not in filter_set, my_list))
如果希望
我的\u列表
中的所有项目都不在过滤器集
中,请注意使用集
,它将使查找速度大大加快(O(N)vs O(1))
您的筛选不正确使用:
filter_set = {'and', 'or', 'of'}
my_list = list(filter(lambda a: a not in filter_set, my_list))
如果希望
我的\u列表
中的所有项目都不在过滤器集
中,请注意使用集
,它将使查找速度大大加快(O(N)vs O(1))
尽管上述答案符合需要,但我认为您打算删除停止语
from nltk.corpus import stopwords
word_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
print(filtered_words)
['land_transport', 'port', 'surveyor', 'organization']
nltk
是Python中最好的资源。你可以用
如果你知道你正在删除真正的英语停止词,你不必做太多的操作
from nltk.corpus import stopwords
word_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
print(filtered_words)
['land_transport', 'port', 'surveyor', 'organization']
Vola尽管上述答案符合需要,但我认为您打算删除停止词
from nltk.corpus import stopwords
word_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
print(filtered_words)
['land_transport', 'port', 'surveyor', 'organization']
nltk
是Python中最好的资源。你可以用
如果你知道你正在删除真正的英语停止词,你不必做太多的操作
from nltk.corpus import stopwords
word_list = ['land_transport', 'and', 'or', 'port', 'of', 'surveyor', 'and', 'organization']
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
print(filtered_words)
['land_transport', 'port', 'surveyor', 'organization']
Vola您的
和“not in a
检查中的”和“not in a检查正在完全按照tin上的说明进行,查找任何不包含和的单词。由于land\u transport
包含和,因此它会被过滤掉。大概您使用的是而不是中的=
是有原因的,但不知道原因是什么,很难告诉你如何解决问题。检查中可能出现的和“not”的重复就是按照tin上所说的做,找到任何不包含和的单词。由于land\u transport
包含和,因此它会被过滤掉。大概您使用的是而不是中的=代码>出于某种原因,但在不知道原因是什么的情况下,很难告诉您如何解决问题。可能是filter/lambda的重复,而不是列表理解?可能是filter/lambda的重复,而不是列表理解?感谢您的回答,尤其是使用集合的部分。关于这一点,您能在您的回答@Netwave中参考任何文档吗?谢谢您的回答,特别是关于set的使用部分。关于这一点,你能在回答@Netwave时参考一些文件吗?