Finding the Index of sorted elements in Python Array

RustyC

I have seen answers to the question:
Is it possible to arrange a numpy array (or python list) by using the indexes of the elements in decreasing order? (eg. Finding the Index of N biggest elements in Python Array / List Efficiently)

A very concise answer seems to be (from above link):

L = array([4, 1, 0, 8, 5, 2])
sorted(range(len(L)), key=lambda i:L[i])

This gives the position (in the original array) of the sorted elements.

8 --> 3
5 --> 4
4 --> 0
2 --> 5
1 --> 1
0 --> 2

So the answer is:

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

What I am after is the position (in the sorted array) of the elements:

L = array([4, 1, 0, 8, 5, 2])

8 --> 0
5 --> 1
4 --> 2
2 --> 3
1 --> 4
0 --> 5

So I want :

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

I realise I could take the answer from the first example, and use that to get what I want (with a bit more fiddling) but is there a short cut?

Efficiency is not a concern. I just need something that gives me the answer.

Peter de Rivaz

You can simply use your technique twice to get the indices in the sorted list:

A=[4, 1, 0, 8, 5, 2]
B=sorted(range(len(A)),key=lambda x:A[x],reverse=True)
C=sorted(range(len(A)),key=lambda x:B[x])
print C

prints

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

Explanation

The idea is that the first iteration produces a list:

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

giving the locations in the original list of the sorted sequence.

In other words, A[3]=8 is the largest element in the original list, A[4]=5 is the next largest, etc.

The second stage then sorts these indices in B back into the order 0,1,2,3,4,5 and produces C which contains the index in the list B.

It may help to think of B as sorting the list into descending order, and C as reversing the sort back into the original unsorted order, while keeping track of the indices in the sorted list.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding an index in a sorted array

From Dev

Finding range index of a sorted array on a specific element

From Dev

Finding an index in an array in python

From Dev

Python: finding index of all elements of array in another including repeating arrays

From Dev

Finding mode of a sorted array

From Dev

Finding index of array in 2 multidimensional array in Python

From Dev

Finding index of array in 2 multidimensional array in Python

From Dev

Finding all unique elements from a big sorted array in log n time?

From Dev

Finding all unique elements from a big sorted array in log n time?

From Dev

Finding nonzero elements in an array

From Dev

Finding index in array

From Dev

Fastest way of finding the index of the closest element in a non-sorted Python list of floats

From Dev

Python iterate through array while finding the mean of the top k elements

From Dev

In R, how to produce an array containing sorted index of corresponding elements in another array?

From Dev

Python: How to index the elements of a numpy array?

From Dev

Python: How to index the elements of a numpy array?

From Dev

Finding elements of an array that match elements of a second array

From Dev

Find the original index of sorted array

From Java

Finding sum of elements in Swift array

From Dev

Finding the dimension indexes of elements of an array

From Dev

Finding three elements in an array that are not adjacent

From Java

Index for finding an element in a JSON array

From Dev

Finding index of string in string array

From Dev

Finding the index of a numpy array in a list

From Dev

Finding an unused index in a Verilog array?

From Dev

Finding an index of an element in a string array

From Dev

Bitmap index search results array: finding the indices of nonzero elements in constant time?

From Dev

Finding the index of elements of the same class but with different parents

From Dev

Finding Dom Node Index for elements of a certain class

Related Related

  1. 1

    Finding an index in a sorted array

  2. 2

    Finding range index of a sorted array on a specific element

  3. 3

    Finding an index in an array in python

  4. 4

    Python: finding index of all elements of array in another including repeating arrays

  5. 5

    Finding mode of a sorted array

  6. 6

    Finding index of array in 2 multidimensional array in Python

  7. 7

    Finding index of array in 2 multidimensional array in Python

  8. 8

    Finding all unique elements from a big sorted array in log n time?

  9. 9

    Finding all unique elements from a big sorted array in log n time?

  10. 10

    Finding nonzero elements in an array

  11. 11

    Finding index in array

  12. 12

    Fastest way of finding the index of the closest element in a non-sorted Python list of floats

  13. 13

    Python iterate through array while finding the mean of the top k elements

  14. 14

    In R, how to produce an array containing sorted index of corresponding elements in another array?

  15. 15

    Python: How to index the elements of a numpy array?

  16. 16

    Python: How to index the elements of a numpy array?

  17. 17

    Finding elements of an array that match elements of a second array

  18. 18

    Find the original index of sorted array

  19. 19

    Finding sum of elements in Swift array

  20. 20

    Finding the dimension indexes of elements of an array

  21. 21

    Finding three elements in an array that are not adjacent

  22. 22

    Index for finding an element in a JSON array

  23. 23

    Finding index of string in string array

  24. 24

    Finding the index of a numpy array in a list

  25. 25

    Finding an unused index in a Verilog array?

  26. 26

    Finding an index of an element in a string array

  27. 27

    Bitmap index search results array: finding the indices of nonzero elements in constant time?

  28. 28

    Finding the index of elements of the same class but with different parents

  29. 29

    Finding Dom Node Index for elements of a certain class

HotTag

Archive