patternpythonCriticalCanonical
Getting the index of the returned max or min item using max()/min() on a list
Viewed 0 times
indexmaxreturnedgettingtheusinglistminitem
Problem
I'm using Python's
I need to be able to return the actual index of the min or max value, not just the value.
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.
Solution
Find the minimum value with
Or the maximum:
If your list contains repeats of the minimum or maximum value this will return the index of the first one.
min() then find that value's index with .index():values.index(min(values))
Or the maximum:
values.index(max(values))
If your list contains repeats of the minimum or maximum value this will return the index of the first one.
Context
Stack Overflow Q#2474015, score: 640
Revisions (0)
No revisions yet.