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

Posted by KevinGriffin on Stack Overflow See other posts from Stack Overflow or by KevinGriffin
Published on 2010-03-18T23:20:35Z Indexed on 2010/03/19 0:21 UTC
Read the original article Hit count: 665

Filed under:

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):
        newBoard = currentBoard.newBoardWithMove([i / 3, i % 3], player)

        if newBoard:
            temp = minMax(newBoard, depth + 1, not isMinLevel)  
            values.append(temp)

    if isMinLevel:
        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.

© Stack Overflow or respective owner

Related posts about python