使用列表上的 max()/min() 获取返回的最大或最小项的索引

编程入门 行业动态 更新时间:2024-10-26 13:19:28
本文介绍了使用列表上的 max()/min() 获取返回的最大或最小项的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在列表上使用 Python 的 max 和 min 函数来实现极大极小算法,我需要 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.

推荐答案

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

更多推荐

使用列表上的 max()/min() 获取返回的最大或最小项的索引

本文发布于:2023-11-30 10:41:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649688.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小项   表上   索引   min   max

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!