Skip to content
Advertisement

Return same position for similar values in a list

I have sorted a list in descending order, I am trying to find a way in which similar numbers re-appearing in the list will return equal position and the next unique number to return the next position. Example in this samplelist = [50, 40, 40, 30, 30, 20, 10] 50 should return 1, 40 returns 2, next 40 returns 2, 30 returns 4, next 30 returns 4 then 20 returns 6, 10 returns 7. Note that this will be a dynamic list. This is how far I have gone but it isn’t quite right.

JavaScript

Advertisement

Answer

This can also be done using just the list.index utility in python, which gives the index of the first occurrence of an element.

So, your program must do this :

JavaScript

instead of this :

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement