如何获取列表中从大到小的索引?
How do I get the indexes from the biggest to smallest number in a list?
我正在 Python 编写一个小项目:
我想获取以下列表中从大到小的索引(在列表中):
list = [20, 30, 24, 26, 22, 10]
结果应该是:
index_list = [1, 3, 2, 4, 0, 5]
有人知道我该怎么做吗?
提前致谢。
按升序索引顺序构建 index_list
,然后使用 Comparator
调用 sort()
,按给定索引处的 list
中的值降序排序。
List<Integer> list = Arrays.asList(20, 30, 24, 26, 22, 10);
List<Integer> index = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++)
index.add(i);
index.sort(Comparator.comparing(list::get).reversed());
System.out.println(index);
输出
[1, 3, 2, 4, 0, 5]
或者您可以使用流在一条语句中完成,结果相同:
List<Integer> index = IntStream.range(0, list.size()).boxed()
.sorted(Comparator.comparing(list::get).reversed())
.collect(Collectors.toList());
我对 python 中的编程还很陌生,但这似乎可行:
list = [20, 30, 24, 26, 22, 10]
list_sorted = list.copy()
list_sorted.sort()
list_index = []
for x in list_sorted:
list_index.insert(0,list.index(x))
print(list_index)
输出:
[1, 3, 2, 4, 0, 5]
因为上面会产生不正确的值,如果有重复,请看下一个:
list = [20, 10, 24, 26, 22, 10]
list_tmp = list.copy()
list_sorted = list.copy()
list_sorted.sort()
list_index = []
for x in list_sorted:
list_index.insert(0,list_tmp.index(x))
list_tmp[list_tmp.index(x)] = -1
print(list)
print(list_index)
输出:
[20, 10, 24, 26, 22, 10]
[3, 2, 4, 0, 5, 1]
输出是 [3, 2, 4, 0, 5, 1]
还是 [3, 2, 4, 0, 1, 5]
应该无关紧要,因为这些索引引用相同的值。
已接受的答案不在 Python 中,另一个答案需要几行,而且速度不如更大的数据集快,所以我想我会把帽子扔进去戒指:
import numpy as np
list = [20, 30, 24, 26, 22, 10]
index_list = np.array(list).argsort().tolist()[::-1]
输出确实是:
index_list = [1, 3, 2, 4, 0, 5]
它处理重复值的索引,而且速度非常快。它需要导入,但如果您处理的是大数据集,它可能会具有您需要的其他功能,并且总体上会节省时间。
我正在 Python 编写一个小项目:
我想获取以下列表中从大到小的索引(在列表中):
list = [20, 30, 24, 26, 22, 10]
结果应该是:
index_list = [1, 3, 2, 4, 0, 5]
有人知道我该怎么做吗? 提前致谢。
按升序索引顺序构建 index_list
,然后使用 Comparator
调用 sort()
,按给定索引处的 list
中的值降序排序。
List<Integer> list = Arrays.asList(20, 30, 24, 26, 22, 10);
List<Integer> index = new ArrayList<>(list.size());
for (int i = 0; i < list.size(); i++)
index.add(i);
index.sort(Comparator.comparing(list::get).reversed());
System.out.println(index);
输出
[1, 3, 2, 4, 0, 5]
或者您可以使用流在一条语句中完成,结果相同:
List<Integer> index = IntStream.range(0, list.size()).boxed()
.sorted(Comparator.comparing(list::get).reversed())
.collect(Collectors.toList());
我对 python 中的编程还很陌生,但这似乎可行:
list = [20, 30, 24, 26, 22, 10]
list_sorted = list.copy()
list_sorted.sort()
list_index = []
for x in list_sorted:
list_index.insert(0,list.index(x))
print(list_index)
输出:
[1, 3, 2, 4, 0, 5]
因为上面会产生不正确的值,如果有重复,请看下一个:
list = [20, 10, 24, 26, 22, 10]
list_tmp = list.copy()
list_sorted = list.copy()
list_sorted.sort()
list_index = []
for x in list_sorted:
list_index.insert(0,list_tmp.index(x))
list_tmp[list_tmp.index(x)] = -1
print(list)
print(list_index)
输出:
[20, 10, 24, 26, 22, 10]
[3, 2, 4, 0, 5, 1]
输出是 [3, 2, 4, 0, 5, 1]
还是 [3, 2, 4, 0, 1, 5]
应该无关紧要,因为这些索引引用相同的值。
已接受的答案不在 Python 中,另一个答案需要几行,而且速度不如更大的数据集快,所以我想我会把帽子扔进去戒指:
import numpy as np
list = [20, 30, 24, 26, 22, 10]
index_list = np.array(list).argsort().tolist()[::-1]
输出确实是:
index_list = [1, 3, 2, 4, 0, 5]
它处理重复值的索引,而且速度非常快。它需要导入,但如果您处理的是大数据集,它可能会具有您需要的其他功能,并且总体上会节省时间。