Optimal method to find the max of sublist items within list

后端 未结 3 1271
感动是毒
感动是毒 2020-12-21 01:55

I have a multidimensional list in the format:

list = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]

How do I obtain the maximum value of the third value

3条回答
  •  萌比男神i
    2020-12-21 02:32

    applying max on the list will return the maximum list, which isn't what you want. You could indeed use a list comprehension to extract the third element, and then apply max on that comprehension:

    >>> lst = [[1, 2, 3], [2, 4, 2], [0, 1, 1]]
    >>> max([x[2] for x in lst])
    3
    

提交回复
热议问题