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

Python在具有未定义整数和浮点数的列表和元素列表中查找最大值,python,pandas,list,numpy,nested-lists,Python,Pandas,List,Numpy,Nested Lists,我有一个很大的清单。我正在努力寻找其中的最大值和最小值。前面的问题由带字符串的列表组成,这个问题是不同的 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.flo

我有一个很大的清单。我正在努力寻找其中的最大值和最小值。前面的问题由带字符串的列表组成,这个问题是不同的

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

输出

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]
编辑:获取子列表的长度

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]

您希望从您提供的列表中找到max=9和min=-1吗?另外,您说您有一个大列表,但您的示例是一个列表和整数的“大”列表是的。最大值=9,最小值=-2@tomgalpin我在这里提供了一份样本清单。这能回答你的问题吗?我喜欢你的在线代码:
np.max(np.concatenate([l if isinstance(l,list)else[l]表示大列表中的l])
。这个很好用。上面的
concatenate
效果更好吗?这取决于具体情况,但我认为最好的映射在这里工作我的“大列表”也有元素。您的代码是否只考虑列表的列表?还是考虑列表中的所有值?<代码> max(map(max,[L,如果IsList(L,list)[L]在LigBigList中的L])<代码> >你不需要<代码>列表 >这里:它考虑所有的值