使用列表上的 max()/min() 获取返回的最大或最小项的索引 [英] Getting the index of the returned max or min item using max()/min() on a list

查看:11
本文介绍了使用列表上的 max()/min() 获取返回的最大或最小项的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表上使用 Python 的 maxmin 函数来实现极大极小算法,我需要 max()<返回的值的索引/code> 或 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.

推荐答案

if is_min_level:
    return values.index(min(values))
else:
    return values.index(max(values))

这篇关于使用列表上的 max()/min() 获取返回的最大或最小项的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆