Python len()函数在处理列表时报告’float’错误。

huangapple go评论71阅读模式
英文:

Python len() function reporting 'float' error when handling a list

问题

我有一个列表的列表,我如下初始化:

patches = [[]]

然后我通过patches.append(list_of_x)向主列表追加,通过patches[i].append(x)向子列表追加对象。

当我调用len(patches)时,出现错误“'float' object is not callable”。

我通过type(patches)验证Python仍将其视为列表。

子列表patches[i]中的对象本身具有各种不同的属性,但我认为这应该是无关的(对吗?)。

有没有想法会发生什么样的巫术?

英文:

I have a list of lists that I initiate as follows:

patches = [[]]

and then I append to main list by patches.append(list_of_x), and to the sub lists I append objects by patches[i].append(x).

When I call len(patches) I get error "'float' object is not callable".

I did verify by type(patches) that Python still considers this a list.

The objects within the sub-lists patches[i] themselves have variety of different attributes, but I think that should be irrelevant (right?).

Any idea what kind of witchcraft could be happening?

答案1

得分: 1

我认为你可能在你的代码中之前定义了一个名为 len 的变量。而这个变量很可能是一个浮点数,正如错误所说,你不能调用它。

英文:

I think you might have defined a variable called len before that in your code. And this variable is probably a float, which, as the error says, you can't call.

答案2

得分: 0

不要做补丁 = [[]]

这样做补丁 = []
当你添加时,写入
补丁.append([x,y,x])

就像这样
补丁.append([])

v = []
v.append([2,2,5,8,2])
v.append([6,6,8,8,7])
打印(len(v))

2

英文:

don't do patches = [[]]

do it patches = []
when you add, write
patches.append([x,y,x])

like this
patches.append([])

see

v = []
v.append([2,2,5,8,2])
v.append([6,6,8,8,7])
print(len(v))

2

huangapple
  • 本文由 发表于 2023年2月16日 18:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75470900.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定