简体   繁体   English

检索列表 Position 的最高价值?

[英]Retrieve List Position Of Highest Value?

There is a list with float values, which can differ or not.有一个包含浮点值的列表,可以不同也可以不同。 How can I find the randomly chosen list-index of one of the highest values in this list?如何找到此列表中最高值之一的随机选择列表索引?

If the context is interesting to you: I try to write a solver for the pen&paper game Battleship .如果您对上下文感兴趣:我尝试为纸笔游戏Battleship编写求解器。 I attempt to calculate probabilities for a hit on each of the fields and then want the the solver to shoot at one of the most likely spots , which means retrieving the index of the highest likelyhood in my likelyhood-list and then tell the game engine this index as my choice.我尝试计算每个字段的命中概率,然后希望求解器在最有可能的位置之一射击,这意味着检索我的可能性列表中可能性最大的索引,然后告诉游戏引擎这个索引作为我的选择。 Already the first move shows that it can happen, that there are a lot of fields with the same likelyhood.第一步已经表明它可能发生,有很多领域具有相同的可能性。 In this case it makes sense to choose one of them at random (and not just take always the first or anything like that).在这种情况下,随机选择其中一个是有意义的(而不是总是选择第一个或类似的东西)。

Find the maximum using How to find all positions of the maximum value in a list?使用How to find all positions of maximum value in a list? 找到最大值? . . Then pick a random from the list using random.choice .然后使用random.choice从列表中随机选择一个。

>>> m = max(a)
>>> max_pos = [i for i, j in enumerate(a) if j == m]
>>> random.choice(max_pos)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM