Python Forum
How to sort a list with duplicates and return their unique indices.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to sort a list with duplicates and return their unique indices.
#1
Given a list:

lst = [48,52,35,35,44,35]
I want to sort the list in descending order, so I use this:


sortedlst = sorted(slst, reverse = True)
I also want the original index in a sorted position as well. Like this:

sortedlstindx = [1,0,4,2,3,5]
for that I use this code:

sortedindx = [elem.index(lst) for elem in sorted(lst, reverse = True)]
The result I got is this:

sortedindx = [1,0,4,2,2,2]
How can I modify the code to give the result I want?
It's a shame I cannot use dict or set to help with this problem. No libraries import either sadly.
perfringo write Sep-21-2022, 10:08 AM:
Nobody expects the Spanish Inquisition! Our chief weapon is surprise! Surprise and fear. Fear and surprise. Let me tell you something: when you're looking at your thread tonight and manic silence meets you don't come cryin' to me. Instead do use respective tags while posting code, output and errors (refer to BBCode help). This empowers others to help you. And... Always Look on the Bright Side of Life: I added them this time but if in the future you do it all by yourself I feel happy.
Reply


Messages In This Thread
How to sort a list with duplicates and return their unique indices. - by Echoroom - Sep-21-2022, 09:19 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  help with adding duplicates elements together in a list 2ECC3O 5 2,041 Sep-10-2022, 07:11 AM
Last Post: 2ECC3O
  sorting a list using unicodes acending order, no loops, no sort(), using recursion lrn2codee 14 6,433 Jun-23-2021, 07:33 PM
Last Post: deanhystad
  how to sort a list without .sort() function letmecode 3 3,445 Dec-28-2020, 11:21 PM
Last Post: perfringo
  Dealing with duplicates to an Excel sheet DistraughtMuffin 6 3,278 Oct-28-2020, 05:16 PM
Last Post: Askic
  Get 5 most unique combinations of elements in a 2D list wanttolearn 1 2,316 Sep-24-2020, 02:26 PM
Last Post: buran
  Return the sum of the first n numbers in the list. pav1983 3 4,108 Jun-24-2020, 03:37 AM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,295 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  duplicates nsx200 3 2,441 Nov-12-2019, 08:55 AM
Last Post: nsx200
  Exercise list remove duplicates RavCOder 9 5,283 Oct-23-2019, 04:16 PM
Last Post: jefsummers
  list and sort query arian29 2 2,231 Sep-18-2019, 06:19 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020