我有这样的列表:
stopwords = ['a', 'and', 'is']
还有这样的句子:
sentence = 'A Mule is Eating and drinking.'
预期输出:
reduced = ['mule', 'eating', 'drinking']
到目前为止我已经:
reduced = filter(None, re.match(r'\W+', sentence.lower()))
现在如何过滤掉停用词(注意大写到小写的转换以及标点符号的省略)?
最佳答案
过滤器表达式错误。将其更改为:
>>> reduced = filter(lambda w: w not in stopwords, re.split(r'\W+', sentence.lower()))
第一个参数是过滤条件。另请注意,要分割句子,您需要 re.split
而不是 re.match
。
>>> list(reduced)
['mule', 'eating', 'drinking']
关于python - 从字符串中排除列表中出现的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31101393/