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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:13:21+00:00 2026-05-22T12:13:21+00:00

I have 2 dictionaries, dict1 and dict2 which contain the same keys, but different

  • 0

I have 2 dictionaries, dict1 and dict2 which contain the same keys, but different values for the keys. What I want to do is for each dictionary, sort the values from largest to smallest, and then give each value a rank 1-N, 1 being the largest value. From here, I want to get the difference of the ranks for the values in each dictionary for the same key. For example:

dict1 = {a:0.6, b:0.3, c:0.9, d:1.2, e:0.2}
dict2 = {a:1.4, b:7.7, c:9.0, d:2.5, e:2.0}

# sorting by values would look like this:
dict1 = {d:1.2, c:0.9, a:0.6, b:0.3, e:0.2}
dict2 = {c:9.0, b:7.7, d:2.5, e:2.0, a:1.4}

#ranking the values would produce this:
dict1 = {d:1, c:2, a:3, b:4, e:5}
dict2 = {c:1, b:2, d:3, e:4, a:5}

#computing the difference between ranks would be something like this:
diffs = {}
for x in dict1.keys():
    diffs[x] = (dict1[x] - dict2[x])

#diffs would look like this:
diffs[a] = -2
diffs[b] = 2
diffs[c] = 1
diffs[d] = -2
diffs[e] = 1

I know dictionaries are meant to be random and not sortable, but maybe there is a method to put the keys and values into a list? The main challenges I am facing are getting the keys and values sorted by value (largest to smallest) and then changing the value to its respective rank in the sorted list.

  • 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-22T12:13:22+00:00Added an answer on May 22, 2026 at 12:13 pm

    A simple solution for small dicts is

    dict1 = {"a":0.6, "b":0.3, "c":0.9, "d":1.2, "e":0.2}
    dict2 = {"a":1.4, "b":7.7, "c":9.0, "d":2.5, "e":2.0}
    k1 = sorted(dict1, key=dict1.get)
    k2 = sorted(dict2, key=dict2.get)
    diffs = dict((k, k2.index(k) - k1.index(k)) for k in dict1)
    

    A more efficient, less readable version for larger dicts:

    ranks1 = dict(map(reversed, enumerate(sorted(dict1, key=dict1.get))))
    ranks2 = dict(map(reversed, enumerate(sorted(dict2, key=dict2.get))))
    diffs = dict((k, ranks2[k] - ranks1[k]) for k in dict1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two large dictionaries with unique keys but possibly overlapping values. I want
I have a custom plist file which contains dictionaries. Each dictionary contains information about
I have a dictionary, with 300 key value pairs, where each of the keys
I have 2 Dictionaries (10 entries in Dict1, 30 in Dict2). Right now I'm
If I have these dictionaries: dict1.xaml: <Color x:Key=Color1>Red</Color> dict2.xaml: <SolidColorBrush x:Key=Brush1 Color={StaticResource Color1} />
I have 5 dictionaries and I want a union of their keys. alldict =
I have an NSArray of NSDictionaries each of which has 4 key values. I'm
I have many dictionaries like this: dict1 = {1:[1,2,3],2:[2,3,4]} dict2 = {2:[3,4,5],3:[4,5,6]} I need
I have 4 dictionaries of type Dictionary<string,string> dict1 k1 = v1 k2 = v2
I have two dictionaries of the following structure: a) dict1 = {'a':[ [1,2], [3,4]

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.