You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python处理《麦克白》文本移除标点停用词时触发ValueError问题求解

错误触发原因
  • list.remove(x) 方法的硬性要求是列表中必须存在参数对应的元素,否则就会抛出你遇到的ValueError。你直接全量遍历标点符号、停用词表调用remove,只要任意一个字符/单词没有出现在待处理列表里就会触发报错,即使文本整体有对应停用词,也可能因为单次remove只能删第一个匹配项,后续遍历到同一个停用词时列表里已经没有对应元素,同样会报错。
  • 标点处理逻辑完全不符合预期:你用split()切分后得到的是带标点的单词列表,比如"Hello,"、"Macbeth!"这类整体元素,单独去删除标点符号单个字符","、"!",自然不可能在列表中找到对应元素。
  • 原代码没有做大小写统一:原文中存在大量首字母大写的停用词比如The、I、You,和你小写的停用词表不匹配,也会导致明明存在的停用词无法被识别,调用remove时报错。
修复方案

我们调整处理逻辑:先逐个清洗每个单词的标点、统一转小写,再过滤停用词,全程生成新列表避免直接修改原列表的各种问题,修正后代码如下:

# 增加文件自动关闭的with写法,避免资源泄漏
with open("macbeth.txt", "r") as macbeth:
    contents = macbeth.read().split()  

def remove_uninteresting_stuff(file_contents):
    punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
    uninteresting_words = {"the", "a", "to", "if", "is", "it", "of", "and", "or", "an", "as", "i", "me", "my", 
    "we", "our", "ours", "you", "your", "yours", "he", "she", "him", "his", "her", "hers", "its", "they", "them", 
    "their", "what", "which", "who", "whom", "this", "that", "am", "are", "was", "were", "be", "been", "being", 
    "have", "has", "had", "do", "does", "did", "but", "at", "by", "with", "from", "here", "when", "where", "how", 
    "all", "any", "both", "each", "few", "more", "some", "such", "no", "nor", "too", "very", "can", "will", "just"}
    # 停用词转集合,查询速度比列表快很多
    result = []
    for word in file_contents:
        # 清洗当前单词的所有标点,同时转小写
        cleaned_word = ''.join([char for char in word.lower() if char not in punctuations])
        # 过滤空字符串和停用词
        if cleaned_word and cleaned_word not in uninteresting_words:
            result.append(cleaned_word)
    return result

print(remove_uninteresting_stuff(contents))

核心改动说明

  • 把停用词从列表改为集合,查询效率提升一个数量级,文本量越大效果越明显
  • 逐词处理:先转小写统一格式,再过滤掉所有标点字符,避免标点和单词绑定无法识别的问题
  • 直接生成新的结果列表,完全避免了remove方法的所有前置校验问题和索引错乱问题
  • 新增文件操作的with语法,自动关闭文件句柄,避免资源泄漏

内容的提问来源于stack exchange,提问作者hmood

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 12:54:02