Finding the Max value in a two dimensional Array
Asked Answered
O

8

34

I'm trying to find an elegant way to find the max value in a two-dimensional array. for example for this array:

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

I would like to extract the value '4'. I thought of doing a max within max but I'm struggling in executing it.

Oliverolivera answered 20/11, 2016 at 8:11 Comment(0)
M
43

Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers))  # max numbers from each sublist
[1, 2, 2, 3, 4]

>>> max(map(max, numbers))  # max of those max-numbers
4
Memorize answered 20/11, 2016 at 8:13 Comment(0)
D
44

Another way to solve this problem is by using function numpy.amax()

>>> import numpy as np
>>> arr = [0, 0, 1, 0, 0, 1] , [0, 1, 0, 2, 0, 0] , [0, 0, 2, 0, 0, 1] , [0, 1, 0, 3, 0, 0] , [0, 0, 0, 0, 4, 0]
>>> np.amax(arr)
Dodecanese answered 24/7, 2018 at 11:3 Comment(2)
Better than the accepted answer in every wayPrem
except that it doesn't work on an input like [[0, 1, 2], [3, 4]]Substitute
M
43

Max of max numbers (map(max, numbers) yields 1, 2, 2, 3, 4):

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

>>> map(max, numbers)
<map object at 0x0000018E8FA237F0>
>>> list(map(max, numbers))  # max numbers from each sublist
[1, 2, 2, 3, 4]

>>> max(map(max, numbers))  # max of those max-numbers
4
Memorize answered 20/11, 2016 at 8:13 Comment(0)
E
13

Not quite as short as falsetru's answer but this is probably what you had in mind:

>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
>>> max(max(x) for x in numbers)
4
Excavation answered 21/5, 2018 at 1:34 Comment(0)
F
5
>>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]

You may add key parameter to max as below to find Max value in a 2-D Array/List

>>> max(max(numbers, key=max))
4
Fredra answered 7/6, 2021 at 2:22 Comment(0)
L
4

How about this?

import numpy as np
numbers = np.array([[0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]])

print(numbers.max())

4
Loreeloreen answered 8/4, 2020 at 11:57 Comment(1)
and how about to get the index of each sub-list which has max value?Spermary
S
1

One very easy solution to get both the index of your maximum and your maximum could be :

numbers = np.array([[0,0,1,0,0,1],[0,1,0,2,0,0],[0,0,2,0,0,1],[0,1,0,3,0,0],[0,0,0,0,4,0]])
ind = np.argwhere(numbers == numbers.max()) # In this case you can also get the index of your max
numbers[ind[0,0],ind[0,1]]
Supen answered 1/4, 2021 at 14:57 Comment(0)
P
0

This approach is not as intuitive as others but here goes,

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

maximum = -9999

for i in numbers:

    maximum = max(maximum,max(i))

return maximum"
Parnassus answered 25/6, 2022 at 3:39 Comment(0)
I
0

chain.from_iterable is an elegant solution:

from itertools import chain
numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [
    0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0]
res = max(chain.from_iterable(numbers)) # 4
Inferential answered 10/11, 2023 at 5:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.