14

To find the maximal value in a matrix of numbers, we can code 5 lines to solve the problem:

ans = matrix[0][0]
for x in range(len(matrix)):
    for y in range(len(matrix[0])):
        ans = max(ans, matrix[x][y])
return ans

Is there a one line solution for this problem? The one that I came up with is pretty awkward actually:

return max(max(matrix, key=max))

or

return max(map(max, matrix))
  • Why `key=max` to the `max` function? That doesn't make sense. But otherwise, that works, so you've already answered your own question. You can also use numpy, if you're going to do this often or with large matrices. –  Mar 07 '16 at 06:21
  • @Evert `key=max` in `max` function is because for each row, you use the max of that row to compare. –  Mar 07 '16 at 06:31
  • 1
    Keep in mind that putting a lot of logic into a single line is a great way to make your code unreadable an hard to maintain. – David says Reinstate Monica Mar 07 '16 at 15:02
  • What is a "matrix"? Do you mean a list of lists, or a 2D numpy array? – RemcoGerlich Mar 07 '16 at 16:02

4 Answers4

17

You can use generator expression to find the maximum in your matrix. That way you can avoid building the full list of matrix elements in memory.

maximum = max(max(row) for row in matrix)

instead of list comprehension as given in a previous answer here

maximum = max([max(row) for row in matrix])

This is from PEP (the rationale section):

...many of the use cases do not need to have a full list created in memory. Instead, they only need to iterate over the elements one at a time.

...

Generator expressions are especially useful with functions like sum(), min(), and max() that reduce an iterable input to a single value

...

The utility of generator expressions is greatly enhanced when combined with reduction functions like sum(), min(), and max().

Also, take a look at this SO post: Generator Expressions vs. List Comprehension.

Community
  • 1
  • 1
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
8

By matrix, I assume you mean a 2d-list.

max([max(i) for i in matrix])
  • 21
    You really don't need to create an intermediate list, e.g.: `max(max(i) for i in matrix)` - a generator expression is good enough. – AChampion Mar 07 '16 at 06:26
5

using numpy.amax:

import numpy as np
>>> my_array
array([[1, 2, 3],
       [9, 8, 6]])
>>> np.amax(my_array)
9
Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • The point of the question is that the OP doesn't have the data inside a numpy array. If you plan on answering using a numpy solution at the very least you have to also add the code to perform the conversion from python list of lists to numpy array. – Bakuriu Mar 07 '16 at 14:14
  • @Bakuriu Actually, although this answer in its current form doesn't show it, Numpy will also operate on a list of lists as if it were an `array`, if all the sublists have the same length. (Presumably this is the case because the OP referred to it as a matrix.) That being said, if you don't already have a reason to use Numpy, it's a big dependency that probably isn't worth pulling in for just this one function. – David Z Mar 07 '16 at 15:18
5

You can also flatten your array:

from itertools import chain

flatten = chain.from_iterable

max(flatten(matrix))
GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52