python - 如何获取列表中的第一个最大不同元素?
问题描述
populations = [[0 1]
[1 1]
[0 1]
[0 1]]
score = [0, 6, 4, 4]
best_score = max(score)
我要做的是先chromosome_best
通过,匹配
best_score
并score
获取匹配的索引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
被读取的是和之间的第一个匹配score
项best_score?
解决方案
类似的东西populations[score.index(best_score)]
应该可以解决问题。list.index
返回列表与您提供的元素匹配的第一个索引,因此无论有多少,它将始终与第一个匹配项匹配
推荐阅读
- excel - 有条件地将数据复制和转置到新的 Excel 工作表
- javascript - 带有有效负载 {"name":"Screen2"} 的操作 'NAVIGATE' 未被任何 navigator 处理。React navigation 5
- react-native - 如何解决导航包
- microsoft-graph-api - 如何获取与 Microsoft Teams 会议相关的录音
- css - 有没有办法使用 @push 和 @stack 进行复杂的 laravel 刀片布局?
- javascript - 加载 XHR 响应数据以索引 HTML 页面
- html - HTML 表单只读错误
- python - 石头、纸、剪刀:赋值前引用的变量
- php - MYSQL如何使用变量在“where”中的变量值设置if语句
- python - 生成不重复的二进制序列