[PYTHON] Addictive note: max (max (list)) must not be used when maxing the value of a 2D array

a = []
a.append([-1,-1,-1])
a.append([-1, 2, -1])
a.append([-1, -1, 3])

I want to get the maximum value of 3 in this list when there is a list. In other words

>>> max(list(map(lambda x: max(x), a)))
3

I want to do. Nesting max at this time will not work.

>>> max(max(a))
2

This is a comparison of [-1, -1, -1], [-1, 2, -1] and [-1, -1, 3] when max (a) is performed. Will be done. This is compared in lexicographic order

>>> [-1, 2, -1] > [-1, -1, 3]
True
>>> "acb" > "abz" #With this
True

This is because

Solution 1: map

>>> max(list(map(lambda x: max(x), a)))
3

Solution 2: Make it a flat list in itertools.chain

>>> from itertools import chain
>>> list(chain(*a))
[-1, -1, -1, -1, 2, -1, -1, -1, 3]
>>> max(chain(*a))
3

Recommended Posts

Addictive note: max (max (list)) must not be used when maxing the value of a 2D array
When incrementing the value of a key that does not exist
[Python memo] Be careful when creating a two-dimensional array (list of lists)
Python note: When the pip command cannot be used
Be careful when differentiating the eigenvectors of a matrix
Make a note of the list of basic Pandas usage
The return value (generator) of a function that combines finally and yield must not be passed directly to next
Extract the value of dict or list as a string
Do not specify a mutable object (list type, dictionary type, etc.) as the initial value of the function argument of python.