首页 > 解决方案 > 如何获取列表中的第一个最大不同元素?

问题描述

    populations = [[0 1]
                   [1 1]
                   [0 1]
                   [0 1]]

score = [0, 6, 4, 4] best_score = max(score)

我要做的是先chromosome_best通过,匹配 best_scorescore获取匹配的索引score,然后使用该索引,获取其对应的值populations

到目前为止,我的代码如下所示:

chromosome_best = numpy.array([d for a, d in zip(score, populations) if a == best_score])

但是我怎么能改变它,使得当 score 具有非唯一值时,例如: score = [6, 6, 0, 4] best_score = 6

chromosome_best被读取的是和之间的第一个匹配scorebest_score?

标签: python

解决方案


类似的东西populations[score.index(best_score)]应该可以解决问题。list.index返回列表与您提供的元素匹配的第一个索引,因此无论有多少,它将始终与第一个匹配项匹配


推荐阅读