-2

I have a big list of list. I am trying to find max and min in it. Previous questions on this consists lists with strings and this question is 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]]

My code:

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

Present output:

TypeError: 'numpy.float64' object is not iterable
CDJB
  • 14,043
  • 5
  • 29
  • 55
Mainland
  • 4,110
  • 3
  • 25
  • 56

2 Answers2

2

You could do the following, using max() and min() with generator expressions, and a check with isinstance() to see if each element is a list or not.

>>> 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
CDJB
  • 14,043
  • 5
  • 29
  • 55
1

the problem is that you need the list to contain only lists

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

or

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

Output

9

EDIT: get len of sublist

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

if you only want consider list:

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

We could do as when we got the max:

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]

I think the best way is:

list(map(lambda x: len(x) if isinstance(x,list) else None ,big_list))
#[3, None, 4, None, 3, None, 6, None, 4, 3]
ansev
  • 30,322
  • 5
  • 17
  • 31