TypeError:(“'list'对象不可调用”,“发生在索引0上”)

2024-06-10 01:03:49 发布

您现在位置:Python中文网/ 问答频道 /正文

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 (most recent call last) in

~\Anaconda3\lib\site-packages\pandas\core\frame.py in apply(self, func, axis, broadcast, raw, reduce, 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 in get_result(self) 184 return self.apply_raw() 185 --> 186 return self.apply_standard() 187 188 def apply_empty_result(self):

~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_standard(self) 290 291 # compute the result using the series generator --> 292 self.apply_series_generator() 293 294 # wrap results

~\Anaconda3\lib\site-packages\pandas\core\apply.py in apply_series_generator(self) 319 try: 320 for i, v in enumerate(series_gen): --> 321 results[i] = self.f(v) 322 keys.append(v.name) 323 except Exception as e:

in (x)

in replace_tfidf_words(x, transformed_file, features) 24 ''' 25 dictionary = create_tfidf_dictionary(x, transformed_file, features) ---> 26 return list(map(lambda y:dictionary[f'{y}'], x.title.split()))

TypeError: ("'list' object is not callable", 'occurred at index 0')

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


Tags: inselfdictionaryreturnlibdefresultfile
1条回答
网友
1楼 · 发布于 2024-06-10 01:03:49

这是一个常见错误,通常在为名为list的变量设置某个值时发生。考虑下面的代码:

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

在第一行,我们为名为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不同的名称。如果在任何地方都找不到此代码段,可以替换

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

print(list)

它将显示list变量上的值,并可能帮助您找出它的更改位置

无论哪种方式,您都必须搜索您发布给我们的代码片段之外的内容,这可能是一个挑战

相关问题 更多 >