【发布时间】:2020-01-31 00:58:28
【问题描述】:
我有一个大的浮点数和整数列表,如下所示。我想通过忽略空元素或单个元素来查找每个子列表的长度。
big_list = [[137.83,81.80,198.56],0.0,[200.37,151.55,165.26, 211.84],
0.0,[1,2,3],4,[5,6,0,5,7,8],0,[2,1,4,5],[9,1,-2]]
我现在的密码:
list_len = []
for i in big_list:
list_len.append(len(i))
当前输出:
TypeError: object of type 'numpy.float64' has no len()
预期输出:
list_len = [3,4,3,6,4,3] # list_len should neglect elements like 0, 4 in big_list.
【问题讨论】:
-
检查类型或捕获异常
-
"# list_len 应该忽略 big_list 中的 0、4 等元素" 听起来像是您需要在脚本中构建的一些逻辑
-
这里的pandas有什么用?
-
@MadPhysicist 我只想考虑列表,而不是元素。我想忽略它们。