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

创建Brown语料库训练测试集时遇'list'对象不可调用错误的解决

解决Brown语料库代码中的"'list' object is not callable"错误

你的代码逻辑本身没有问题,出现这个错误的核心原因是你在代码的其他部分(或之前的交互式会话里)把内置函数list当成变量名赋值过——比如写过list = [1,2,3]这类代码,导致Python把list识别成你定义的列表对象,而非用来创建列表的内置函数,后续调用list()时就会抛出"list object is not callable"的错误。

解决方案

  • 检查所有代码上下文,找到把list作为变量名的地方,将变量名改成其他不冲突的名称(比如my_tag_list、sent_list)。
  • 如果是在交互式终端或Jupyter Notebook中运行,直接重启解释器/内核,即可重置list的内置函数引用。

修正后的代码示例

import math
from random import shuffle
from nltk.corpus import brown  # 补充你可能遗漏的Brown语料库导入

sentences = brown.tagged_sents(tagset='universal') 
# 正常生成标签类型集合
tag_types = list(set([token[1] for sent in sentences for token in sent]))

split_index = math.floor(0.8 * len(sentences))

# 转成列表并打乱
sentences_list = list(sentences)
shuffle(sentences_list)

train = sentences_list[0:split_index]
test = sentences_list[split_index:]

print('Number of train sentences: ' + str(len(train)))
print('Number of test sentences: ' + str(len(test)))

注:你的原始代码可能遗漏了from nltk.corpus import brown这行导入,否则无法引用brown对象,建议补上。

内容的提问来源于stack exchange,提问作者goht.cheez

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 03:43:22