Python Forum
Use ranking function for different lists
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Use ranking function for different lists
#1
Hi.

I have a function that i use to rank a list from highest to lowest, right now it is hard coded to one list, what i would like to be able to do is pass lists to it.


def rank():
    global mons
    dict = {}
    for i, e in enumerate(mons):                   
        dict[e] = i                                
    rank = 1                                      
    for key in sorted(dict.keys(), reverse=TRUE):
        mons[dict.get(key)] = rank                 
        rank = rank + 1       
Reply
#2
renamed dict to thedict, otherwise you will be redefining python's dict

def rank(mons):
    thedict = {}
    for i, e in enumerate(mons):                   
        thedict[e] = i                                
    rank = 1                                      
    for key in sorted(thedict.keys(), reverse=True):
        mons[thedict.get(key)] = rank                 
        rank = rank + 1  
    return thedict
  
print(rank(['1','6','4','3','1']))
Output:
{'1': 4, '6': 1, '4': 2, '3': 3}
Reply
#3
Sure i can do that, but that dooesn't solve my issue.
Reply
#4
(Feb-15-2022, 12:30 PM)klatlap Wrote: Sure i can do that, but that dooesn't solve my issue.

It's unclear what issue you are trying to solve. For starters - what you are trying to accomplish? What rank functions should do?
Gribouillis and Larz60+ like this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
I wanted to have this as a function that i could call and use on different lists, maybe i am unable to do that and will just have to reuse this code for each list.
Reply
#6
What is wrong with Larz60's example? It does the same thing as yours, but you pass the list as an argument instead of it being hard coded. One ranker that can be used with any list.

I modified it a little to rank numbers instead of strings, and to return a list of "rankings".
def rank(values):
    valuemap = {value:index for index, value in enumerate(values)}
    result = [0] * len(values)
    for rank, value in enumerate(sorted(values, reverse=True)):
        result[valuemap[value]] = rank+1
    return result

print(rank([2, 5, 3, 6, 1, 4]))
Output:
[5, 2, 4, 1, 6, 3]
A problem with this is any duplicate values are removed because they are used as keys in the valuemap dictionary. But if this is what you want, numpy.argsort() does the same thing without the unique value requirement.
import numpy

values = ['1','6','4','3','1']
indices = numpy.argsort(values)
print(indices)
print([values[index] for index in indices])
Output:
[0 4 3 2 1] ['1', '1', '3', '4', '6']
Reply
#7
Sorry guys, i think it was my misunderstanding, thanks for all your help, cheers laz
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pop function for lists jamesaarr 8 2,661 Aug-26-2021, 06:40 PM
Last Post: ndc85430
  drf ordering by custom field and add ranking in to_representation tomfong521 0 1,869 Mar-24-2021, 09:56 AM
Last Post: tomfong521
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,387 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Reading Multiple Lists Using SUM function dgrunwal 6 3,360 Jun-03-2020, 08:23 PM
Last Post: dgrunwal
  Money conversion - problems with lists and .format function fatherted99 1 1,825 Mar-12-2020, 06:29 PM
Last Post: ndc85430
  Using function argument in lists comprehension. blackknite 5 3,066 Apr-23-2019, 09:59 PM
Last Post: snippsat
  sort lists of lists with multiple criteria: similar values need to be treated equal stillsen 2 3,293 Mar-20-2019, 08:01 PM
Last Post: stillsen
  Help with plot, how to show ranking with boxes icebelt 1 2,460 Jan-25-2019, 10:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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