Adding a ranking based on two fields
jenn.duerr at gmail.com
Wed Sep 7 21:33:03 CEST 2011
On Aug 25, 4:53 pm, Chris Rebert <c... at rebertia.com> wrote:
> On Thu, Aug 25, 2011 at 1:38 PM, noydb <jenn.du... at gmail.com> wrote:
> > Hello All,
> > Looking for some advice/ideas on how to implement arankingto 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 uniqueranking, 1 through the number of
> > records (thousands). The rank isbasedon 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, addranking... 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
Thanks for this!
Someone passed this along, too - helpful
More information about the Python-list