Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the index of the returned max or min item using max()/min() on a list

Tags:

python

list

max

min

I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at a first player's turn) or min (second player) value.

for i in range(9):     new_board = current_board.new_board_with_move([i / 3, i % 3], player)      if new_board:         temp = min_max(new_board, depth + 1, not is_min_level)           values.append(temp)  if is_min_level:     return min(values) else:     return max(values) 

I need to be able to return the actual index of the min or max value, not just the value.

like image 591
Kevin Griffin Avatar asked Mar 18 '10 23:03

Kevin Griffin


People also ask

How do you return the index of max value in a list?

index() functions to find out the index of the maximum value in a list. Use the enumerate() function to find out the index of the maximum value in a list. Use the numpy. argmax() function of the NumPy library to find out the index of the maximum value in a list.

How do you get the index of the max in a list Python?

The max() method is used to find the maximum value when a sequence of elements is given. It returns that maximum element as the function output. It accepts the sequence as the function argument. The index() method is used to find the index of a given element from a python list.

How do you find the max and min index in Python?

In python is very easy to find out maximum, minimum element and their position also. Python provides different inbuilt function. min() is used for find out minimum value in an array, max() is used for find out maximum value in an array. index() is used for finding the index of the element.


1 Answers

Say that you have a list values = [3,6,1,5], and need the index of the smallest element, i.e. index_min = 2 in this case.

Avoid the solution with itemgetter() presented in the other answers, and use instead

index_min = min(range(len(values)), key=values.__getitem__) 

because it doesn't require to import operator nor to use enumerate, and it is always faster(benchmark below) than a solution using itemgetter().

If you are dealing with numpy arrays or can afford numpy as a dependency, consider also using

import numpy as np index_min = np.argmin(values) 

This will be faster than the first solution even if you apply it to a pure Python list if:

  • it is larger than a few elements (about 2**4 elements on my machine)
  • you can afford the memory copy from a pure list to a numpy array

as this benchmark points out: enter image description here

I have run the benchmark on my machine with python 2.7 for the two solutions above (blue: pure python, first solution) (red, numpy solution) and for the standard solution based on itemgetter() (black, reference solution). The same benchmark with python 3.5 showed that the methods compare exactly the same of the python 2.7 case presented above

like image 188
gg349 Avatar answered Oct 18 '22 01:10

gg349