-3

I have calculated the histogram of different pixels using the following statement:

pixel_histogram = [float(x)/float(number_of_pixels) for x in pixel_frequency]

If I want to return the maximum element in the list, I would simply do the following:

max(pixel_histogram)

How can I return the index of this maximum element?

Thanks.

Simplicity
  • 47,404
  • 98
  • 256
  • 385
  • try `np.argmax()` or `pixel_histogram.index(max(pixel_histogram))` – Ghilas BELHADJ Jan 30 '18 at 14:58
  • 4
    Possible duplicate of [Pythonic way to find maximum value and its index in a list?](https://stackoverflow.com/questions/6193498/pythonic-way-to-find-maximum-value-and-its-index-in-a-list) – Chris_Rands Jan 30 '18 at 15:01

2 Answers2

0

You can use enumerate():

max_index, max_value = max(enumerate(pixel_histogram), key=lambda x: x[1])

Example:

>>> l = [1, 3, 6, 2, 3, 5]
>>> max_index, max_value = max(enumerate(l), key=lambda item: item[1])
>>> max_value
6
>>> max_index
2
ettanany
  • 19,038
  • 9
  • 47
  • 63
0

If you want the first occurence of the maximum element in the list, you can use

pixel_histogram.index(max(pixel_istogram))

oherwise, you should specify the parameters that you want to consider for choosing which "max" is correct

Gsk
  • 2,929
  • 5
  • 22
  • 29