0

I am coding a little project in Python:

I want to get the indexes (in a list) from the highest to smallest number in the following list:

list = [20, 30, 24, 26, 22, 10]

The outcome should be:

index_list = [1, 3, 2, 4, 0, 5]

Anyone has an idea of how I could do that? Thanks in advance.

Hoori M.
  • 700
  • 1
  • 7
  • 21
  • This is equivalent to generating the indices as a parallel list (which can be as simple as `list(range(len(x)))`, and then rearranging it in the same way that the original list would get sorted. Incidentally, [do not use `list`](https://stackoverflow.com/questions/31087111) as a name for the list, as that will prevent the parallel-list-making code from working :) – Karl Knechtel Apr 12 '23 at 12:29

3 Answers3

1

I am pretty new to programming in python, but this seems to work:

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)

output:

[1, 3, 2, 4, 0, 5]

Because above will produce incorrect values it there are duplicates, see next:

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)

output:

[20, 10, 24, 26, 22, 10]
[3, 2, 4, 0, 5, 1]

It should not matter if output is [3, 2, 4, 0, 5, 1] or [3, 2, 4, 0, 1, 5] because those indexes refer to the same values.

Luuk
  • 12,245
  • 5
  • 22
  • 33
0

The accepted answer isn't in Python and the other answer takes a few lines and isn't as fast as it could be for bigger datasets, so I thought I'd throw my hat in the ring:

import numpy as np

list = [20, 30, 24, 26, 22, 10]
index_list = np.array(list).argsort().tolist()[::-1]

The output is indeed:

index_list = [1, 3, 2, 4, 0, 5]

It handles indices for duplicate values, and it's very fast. It requires an import, but if you're dealing with big datasets chances are it'll have other functionality you need anyway and it'll save time overall.

Brian
  • 1,988
  • 1
  • 14
  • 29
-1

Build the index_list in ascending index order, then call sort() with a Comparator that sorts descending by the value in the list at the given index.

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);

Output

[1, 3, 2, 4, 0, 5]

Or you can do it in one statement using streams, same result:

List<Integer> index = IntStream.range(0, list.size()).boxed()
                               .sorted(Comparator.comparing(list::get).reversed())
                               .collect(Collectors.toList());
Andreas
  • 154,647
  • 11
  • 152
  • 247
  • OP wrote: `I am coding a little project in Python`, this does not look like python? – Luuk Jan 11 '20 at 18:39
  • hmmm, it's JAVA, when you change the input to `20, 10, 24, 26, 22, 10`, the output of your code is `[3, 2, 4, 0, 1, 5]`, and indeed my python thing says `[3, 2, 4, 0, 1, 1]`, remains my question how to do this in python... (but I will figure this out some day!) – Luuk Jan 11 '20 at 19:04
  • your answer is in Java, not Python! – Hoori M. Jan 21 '21 at 02:12