Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Adding a ranking based on two fields

7 views
Skip to first unread message

noydb

unread,
Aug 25, 2011, 4:38:28 PM8/25/11
to
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!

Chris Rebert

unread,
Aug 25, 2011, 4:53:34 PM8/25/11
to noydb, pytho...@python.org

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

noydb

unread,
Sep 7, 2011, 3:33:03 PM9/7/11
to
On Aug 25, 4:53 pm, Chris Rebert <c...@rebertia.com> wrote:
> On Thu, Aug 25, 2011 at 1:38 PM, noydb <jenn.du...@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
>
> Cheers,
> Chris
> --http://rebertia.com

Thanks for this!

Someone passed this along, too - helpful
http://wiki.python.org/moin/HowTo/Sorting
0 new messages