Adding a ranking based on two fields
Chris Rebert
clp2 at rebertia.com
Thu Aug 25 16:53:34 EDT 2011
On Thu, Aug 25, 2011 at 1:38 PM, noydb <jenn.duerr at gmail.com> wrote:
> 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
--
http://rebertia.com
More information about the Python-list
mailing list