Python getting ranks of elements in list weird behavior

ATidedHumour

I have a list of values, I want to get the ranks of these values in the list x

This is the code that I tried:

x = [4,7,9,10,6,11,3,15,2]
seq = sorted(x, reverse=True)
index = [x.index(v) for v in seq]

and I don't understand why it gives this output : [6, 4, 3, 2, 5, 1, 7, 0, 8] instead of the right order.

EDIT: sorry, the right order is [7,5,3,2,1,4,0,6,8] (decending order of the values in x by index)

EDIT2: I fixed it. Sorry, lack of sleep :p.

blhsing

You should iterate over the sorted list seq and find the index of the original list x instead:

x = [4,7,9,10,6,11,3,15,2]
seq = sorted(x, reverse=True)
index = [x.index(v) for v in seq]

index becomes:

[7, 5, 3, 2, 1, 4, 0, 6, 8]

Using the index method in a loop makes the solution unnecessarily O(n^2) in time complexity, however. You should instead consider creating a dict that maps items in x to their indices, and since the lookup of a dict key costs O(1) in average, doing it in a loop would thus cost only O(n):

x = [4,7,9,10,6,11,3,15,2]
indices = {n: i for i, n in enumerate(x)}
seq = sorted(x, reverse=True)
index = [indices[v] for v in seq]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Weird behavior in python list concatenation

From Dev

Weird Behavior When Slicing a List in Python

From Dev

Getting elements in Python list

From Dev

Weird behavior of (boolean list) and (another boolean list) in Python

From Dev

Python datetime weird behavior

From Dev

python thread weird behavior

From Dev

python dictionary weird behavior

From Dev

Set weird behavior (python)

From Dev

Weird scoping behavior in python

From Dev

Removing item in list in Python3 as a for loop - weird behavior

From Dev

Weird razor behavior with List<dynamic>

From Dev

Weird behaviour when iterating through list and deleting elements in python

From Dev

list operations for getting common elements in python

From Dev

Getting index of structured elements of a list in Python

From Dev

Getting elements with dynamic range of list in python

From Dev

getting an attribute from a list of elements in python

From Dev

getting the last and first few elements in a list in python

From Dev

Weird behavior, elements move when slider scrolls

From Dev

Weird behavior of CSS (elements are rotated for 180%)

From Dev

Weird behavior in Python in array initialization

From Dev

Python, weird memory consumption behavior

From Dev

The weird behavior of the print function in python

From Dev

Weird python file path behavior

From Dev

Weird sets behavior in python REPL

From Dev

Weird behavior with a lot of numbers python

From Dev

Weird behavior while looping big list of words

From Dev

Weird behavior dereferencing std::list iterator

From Dev

SwiftUI static List weird reuse behavior

From Dev

Weird behavior of pointer of pointer in a linked list

Related Related

HotTag

Archive