Adding a ranking based on two fields

N

noydb

Hello All,

Looking for some advice/ideas on how to implement a ranking to a
'scores' field I have. So this scores field has values ranging from
1.00-4. There is also a count field. I want to add a rank field such
that all the records have a unique ranking, 1 through the number of
records (thousands). The rank is based on the score first, the count
second. So, a record with a score of 4 and a count of 385 is ranked
higher than a record with a score of 4 and a count of 213 AND higher
than record with a score of 3.25 with a count of 4640.

My thought was to add the unique score values to a list and loop thru
the list... sort records with score=listItem, add ranking... not quite
sure how to do this!

Any help would be much appreciated!
 
C

Chris Rebert

Hello All,

Looking for some advice/ideas on how to implement a ranking to a
'scores' field I have.  So this scores field has values ranging from
1.00-4.  There is also a count field.  I want to add a rank field such
that all the records have a unique ranking, 1 through the number of
records (thousands).  The rank is based on the score first, the count
second.  So, a record with a score of 4 and a count of 385 is ranked
higher than a record with a score of 4 and a count of 213 AND higher
than record with a score of 3.25 with a count of 4640.

My thought was to add the unique score values to a list and loop thru
the list... sort records with score=listItem, add ranking... not quite
sure how to do this!

things = getListOfYourThings()
things.sort(reverse=True, key=lambda item: (item.score, item.count))
for i, thing in enumerate(things):
thing.rank = i + 1

Cheers,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top