Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

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

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() .

(我在清单上使用Python的maxmin函数进行minimax算法,并且需要max()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.

(我需要能够返回最小值或最大值的实际索引,而不仅仅是返回值。)

  ask by Kevin Griffin translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

(假设您有一个列表values = [3,6,1,5] ,并且需要最小元素的索引,在这种情况下,即index_min = 2 。)

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

(避免使用其他答案中出现的itemgetter()解决方案,而改用)

index_min = min(xrange(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() .

(因为它不需要import operator或使用enumerate ,并且它总是比使用itemgetter()的解决方案更快(下面的基准itemgetter() 。)

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

(如果您正在处理numpy数组或可以负担numpy作为依赖项,请考虑同时使用)

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:

(即使在以下情况下将其应用于纯Python列表,也将比第一个解决方案更快。)

  • it is larger than a few elements (about 2**4 elements on my machine)

    (它大于几个元素(我的机器上大约2 ** 4个元素))

  • you can afford the memory copy from a pure list to a numpy array

    (您可以负担得起从纯列表到numpy数组的内存副本)

as this benchmark points out:

(正如该基准所指出的:) 在此处输入图片说明

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).

(我已经使用python 2.7在机器上运行了基准测试,用于上述两个解决方案(蓝色:纯python,第一个解决方案)(红色,numpy解决方案)以及基于itemgetter()的标准解决方案(黑色,参考解决方案)。)

The same benchmark with python 3.5 showed that the methods compare exactly the same of the python 2.7 case presented above

(与python 3.5相同的基准测试表明,这些方法与上述python 2.7情况完全相同)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...