Python 在具有未定义整数和浮点数的列表和元素列表中查找最大值

Python find max in list of lists and elements with undefined number of integers and floats

我有一长串清单。我试图在其中找到最大值和最小值。之前关于此的问题由带字符串的列表组成,这个问题是 differen.t

    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]]

我的代码:

max =  max(list(map(max,boxs_list)))

当前输出:

TypeError: 'numpy.float64' object is not iterable

您可以执行以下操作,将 max()min() 与生成器表达式结合使用,并使用 isinstance() 检查每个元素是否为列表。

>>> min(sl if not isinstance(sl, list) else min(sl) for sl in big_list)
-2
>>> max(sl if not isinstance(sl, list) else max(sl) for sl in big_list)
9

问题是您需要列表只包含列表

np.max(np.concatenate([l if isinstance(l,list) else [l] for l in big_list]))

max(map(max,[l if isinstance(l,list) else [l] for l in big_list]))

输出

9

编辑:获取子列表的长度

lens = [len(l) if isinstance(l,list) else 1 for l in big_list]
#[3, 1, 4, 1, 3, 1, 6, 1, 4, 3]

如果您只想考虑列表:

#lens = [len(l) if isinstance(l,list) else None for l in big_list]
#[3, None, 4, None, 3, None, 6, None, 4, 3]

我们可以像得到最大值时那样做:

list(map(len,[l if isinstance(l,list) else [l] for l in big_list]))
#[3, 1, 4, 1, 3, 1, 6, 1, 4, 3]

我认为最好的方法是:

list(map(lambda x: len(x) if isinstance(x,list) else None ,big_list))
#[3, None, 4, None, 3, None, 6, None, 4, 3]