【问题标题】:TypeError: ("'list' object is not callable", 'occurred at index 0')TypeError: ("'list' object is not callable", '发生在索引 0')
【发布时间】:2020-02-19 12:00:49
【问题描述】:
def create_tfidf_dictionary(x, transformed_file, features):

    vector_coo = transformed_file[x.name].tocoo()
    vector_coo.col = features.iloc[vector_coo.col].values
    dict_from_coo = dict(zip(vector_coo.col, vector_coo.data))
    return dict_from_coo

def replace_tfidf_words(x, transformed_file, features):

    dictionary = create_tfidf_dictionary(x, transformed_file, features)   
    return list(map(lambda y:dictionary[f'{y}'], x.title.split()))
%%time
replaced_tfidf_scores = file_weighting.apply(lambda x: replace_tfidf_words(x, transformed, features), axis=1)

运行此代码时出现以下错误。

TypeError Traceback(最近调用 最后)在

~\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func、轴、广播、原始、减少、result_type、args、**kwds)6911 kwds=kwds, 6912) -> 6913 return op.get_result() 6914 6915 def applymap(self, func):

~\Anaconda3\lib\site-packages\pandas\core\apply.py 在 get_result(self) 第184章 185 --> 186 返回 self.apply_standard() 187 188 def apply_empty_result(自我):

~\Anaconda3\lib\site-packages\pandas\core\apply.py 在 应用标准(自我) 290 291 # 使用序列生成器计算结果 --> 292 self.apply_series_generator() 293 294 # 包装结果

~\Anaconda3\lib\site-packages\pandas\core\apply.py 在 apply_series_generator(self) 319尝试: 320 for i, v in enumerate(series_gen): --> 321 个结果[i] = self.f(v) 322 键.append(v.name) 323 例外为 e:

在 (x) 中

在 replace_tfidf_words(x, 转换文件,特征) 24''' 25 字典 = create_tfidf_dictionary(x,transformed_file,特征) ---> 26 返回列表(map(lambda y:dictionary[f'{y}'], x.title.split()))

TypeError: ("'list' object is not callable", '发生在索引 0')

我是python新手,请帮我解决这个错误。

【问题讨论】:

  • 请改进您问题的格式。很难看到发生了什么。特别是我不知道%%time 行在做什么。
  • 在此之前检查您的代码方式 - 我相信您可能在某处有 list = ,从而覆盖内置名称('list' object is not callable 建议名称 list 不是可调用的 - 当它通常应该)。

标签: python pandas numpy machine-learning


【解决方案1】:

这是一个常见错误,通常在您为名为@9​​87654321@ 的变量设置一些值时发生。考虑下面的代码:

list = [1+1, 2-4, 3*2]
values = list(1)

在第一行,我们为名为@9​​87654323@ 的变量赋值。这有点直观,因为,如果我们有一个 list,为什么不把它放在一个名为 list 的变量中,对吧?然而,在第二行,我们尝试调用函数list()。但是,我们替换了变量list 的值,它不再是函数,它是其他东西。结果是错误:

Traceback (most recent call last):
  File "sl.py", line 3, in <module>
    values = list(1)
TypeError: 'list' object is not callable

在您的示例中,很明显您尝试在此行调用该函数:

return list(map(lambda y:dictionary[f'{y}'], x.title.split()))

挑战在于找到将值分配给list 变量的位置。正如有人评论的那样,您可以在代码中查找list = 子字符串,然后将变量的名称更改为与list 不同的名称。如果你在任何地方都没有找到这个sn -p,你可以替换

return list(map(lambda y:dictionary[f'{y}'], x.title.split()))

通过

print(list)

它将显示您在 list 变量上拥有的值,并可能帮助您确定更改的位置。

无论哪种方式,您都必须在您发布给我们的 sn-p 之外进行搜索,这可能是一个挑战。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 2018-03-31
    • 2021-10-22
    • 1970-01-01
    • 2018-08-21
    • 2021-01-18
    • 2020-12-15
    相关资源
    最近更新 更多