python - 获取列表中最小项目的索引

标签 python list

我想获取列表中最小项目的索引,但以防平局。我想使用两个最小项目的位置并在另一个列表中比较这些位置。

order = [4, 1 ,2 ,1 ]
LPT = [20, 10, 5, 20]
new_order = []

我想从订单列表中的最小值获取索引,如果出现平局,则使用 LPT 中同一位置的最大值。

new_order 应该是这样的:

new_order = [3,1,2,0]

最佳答案

使用列表理解:

[x for x, _ in sorted(enumerate(zip(order, LPT)), key=lambda x: (x[1][0], -x[1][1]))]

代码:

order = [4, 1, 2, 1]
LPT = [20, 10, 5, 20]

new_order = [x for x, _ in sorted(enumerate(zip(order, LPT)), key=lambda x: (x[1][0], -x[1][1]))]
# [3, 1, 2, 0]

关于python - 获取列表中最小项目的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55662545/

相关文章:

python - 如何在 python 中将 for 循环的增量设置为 1?

Python Regex 查找连字符后非数字范围的匹配组,如果范围不存在则忽略模式的其余部分

python - 如何在公式中使用 tensorflow 张量值?

python - 如何通过字典进行测试

python - 在python中将txt文件读取到不带换行符的列表中

C++ 列表 - 添加项目

python - 修复 Python 中的错误?

python - 使用 AngularJS 在 Tornado 中进行 csrf 检查

python - 对于flutter/dart http.Client()不能持久连接

python - 为什么在 Python 中列表访问 O(1)?