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 881453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:15:45+00:00 2026-05-15T12:15:45+00:00

I am just a beginner in python. I have document score= {1:0.98876, 8:0.12245, 13:0.57689}

  • 0

I am just a beginner in python. I have document score= {1:0.98876, 8:0.12245, 13:0.57689} which is stored in dictionary. The keys are corresponding to a series of document id and the values are corresponding to the score for each document id. How do I rank the document based on the scores?

inverse=[(value, key) for key, value in score.items()]
fmax=max(inverse)

I already found the maximum values by using the method above which return:

(0.98876,1)

But what I want is to rank the documents and store in a list:

{(0.98876,1),(0.57689,13),(0.12245,8)}
  • 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-15T12:15:45+00:00Added an answer on May 15, 2026 at 12:15 pm
    sorted(score.items(), key=lambda x:-x[1])
    

    should do the trick

    The order of the elements in a dictionary is not defined, so the result of the sorting has to be stored in a list (or an OrderedDict).

    You should convert it to a list of tuples using items(). With sorted() you can sort them, the key parameter tells it to sort according to the inverse of the second tuple element.

    Full example:

    >>> score= {1:0.98876, 8:0.12245, 13:0.57689}
    >>> sorted(score.items(), key=lambda x:-x[1])
    [(1, 0.98875999999999997), (13, 0.57689000000000001), (8, 0.12245)]
    >>> print [(y,x) for (x,y) in _]
    [(0.98875999999999997, 1), (0.57689000000000001, 13), (0.12245, 8)]
    

    This also shows how to reverse the elements in the tuple if you really want to do that.

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

Sidebar

Related Questions

I'm just a beginner in SQL Server database development and was wondering which authentication
Ok, I am beginner in JAVA. I have just started. I downloaded Java SE
A beginner question, bear with me: I'm just wondering under what circumstances one should
just a quick question: I am a CS undergrad and have only had experience
Just how much slower are events? I have written a streaming XML parser (that
I've got a question related to a beginner Python snippet I've written to introduce
First, let me say that I'm a complete beginner at Python. I've never learned
Beginner in python, but been programming for about 5 years now. I suspect I
I have a basic grasp of XML and python and have been using minidom
First things first, I am a Python beginner, with a typical C++/Java background for

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.