Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 690437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:26:10+00:00 2026-05-14T02:26:10+00:00

I want to allocate rankings to users, based on a points field. Easy enough

  • 0

I want to allocate rankings to users, based on a points field.

Easy enough you’d think with an order_by query. But how do I deal with the situation where two users have the same number of points and need to share the same ranking? Should I use annotate to find users with the same number of points?

My current code, and a pseudocode description of what I’d like to do, are below.

    top_users = User.objects.filter(problem_user=False).order_by('-points_total')
        # Wrong - in pseudocode, this should be 
        # Get the highest points_total, find all the users with that points_total,
        # if there is more than one user, set status to 'Joint first prize',
        # otherwise set status to 'First prize'
    top_users[0].status = "First prize"
    if (top_users[1]): 
            top_users[1].status = "Second prize"
    if (top_users[2]): 
            top_users[2].status = "Third prize"
    if (top_users[3]):
            top_users[3:].status = "Highly commended"

The code above doesn’t deal with the situation where two users have the same number of points and need to share second prize. I guess I need to create a query that looks for unique values of points_total, and does some kind of nested ranking?

It also doesn’t cope with the fact that sometimes there are fewer than 4 users – does anyone know how I can do (in pseudocode) ‘if top_users[1] is not null…’ in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T02:26:11+00:00Added an answer on May 14, 2026 at 2:26 am

    I’d just use itertools.groupby. Something like:

    top_users = [(k, list(g)) for k,g in groupby(top_users, key=lambda x: x.score))]
    for u in top_users[0][1]:
        u.status = 'First prize'
    for u in top_users[1][1]:
        u.status = 'Second prize'
    for u in top_users[2][1]:
        u.status = 'Third prize'
    for score, users in top_users[3:]:
        for u in users:
            u.status = 'Highly recommended'
    

    Or even better, use itertools.count instead of the 4 loops:

    top_users = [(k, list(g)) for k,g in groupby(top_users, key=lambda x: x.score))]
    for c, (score, group) in zip(count(0), top_users):
        if c == 0:
            prize = 'First prize'
        elif c == 1:
            prize = 'Second prize'
        elif c == 2:
            prize = 'Third prize'
        else:
            prize = 'Highly recommended'
        map(lambda x: setattr(x, 'status', prize), group)
    

    And the last refinement, maybe keep a prize list instead of the if statements.

    top_users = [(k, list(g)) for k,g in groupby(top_users, key=lambda x: x.score))]
    prize_list = ['First prize', 'Second prize', 'Third prize', 'Highly recommended']
    for c, (score, group) in zip(count(0), top_users):
        prize = prize_list[c] if c < len(prize_list) else prize_list[-1]
        map(lambda x: setattr(x, 'status', prize), group)
    

    The caveat of this approach is that you’re not doing the grouping in the database, but instead you’d be doing it in memory. This may be a problem if there are a lot of users. See How to query as GROUP BY in django? for some guidance on how to do this in the database.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When users are signing up to our service, I want to allocate one solr
I have a situation where I want to allocate a single NSString object, and
Is the following inefficient? I want to allocate nearly all resources to threads but
I want to allocate a row of a table to any user after they
Help. TCHAR* b; TCHAR* c=TEXT(qwerty); I want to allocate memory and copy content of
I want to statically allocate the array. Look at the following code, this code
In C++, if you want to dynamically allocate an array, you can do something
I want to allocate memory on the stack. Heard of _alloca / alloca and
I want to allocate a memory buffer and initialize it with data of a
Possible Duplicate: Why would you ever want to allocate memory on the heap rather

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.