-2

I do have dictionary with descending order of values as below:

d = {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1 }

I would like to print the ascending oder of Top 3 keys for the descending order of values (Top 3 values). If the values have the same value appearing in the top 3 list, print those relevant keys as below: Expected Output

A1
A2
A3
A6

I have tried something like below: but I am not successful in getting printed "A1" in the output: >>> d = {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1 } >>> for x in sorted(list(d)[0:3]): ... print(x) ... A2 A3 A6

VijZ
  • 69
  • 2
  • 9
  • 2
    Does this answer your question? [How do I sort a dictionary by value?](https://stackoverflow.com/questions/613183/how-do-i-sort-a-dictionary-by-value) – RichieV Sep 21 '20 at 20:06
  • 1
    You will have to write some code to do this. I suggest you break it into smaller steps and solve each of those. For example, first figure out how to sort the keys by their value. – Code-Apprentice Sep 21 '20 at 20:07
  • My Apologies, I do have a sorted dictionary already with values in descending order. My need is different – VijZ Sep 21 '20 at 20:19

2 Answers2

0
result_list = sorted(d.keys(), key=lambda x: int(x[1:]))

Should have your keys sorted in result_list

you can print them line by line using:

for key in result_list:
    print(key)
Yannick Funk
  • 1,319
  • 10
  • 23
  • I'm not getting the exact result set as requested: >>> d = {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1 } >>> result_list = sorted(d.keys(), key=lambda x: int(x[1])) >>> for key in result_list: ... print(key) ... A1 A10 A2 A3 A5 A6 >>> – VijZ Sep 21 '20 at 20:16
  • It was a bug in list slicing, I edited my answer, now its fixed – Yannick Funk Sep 21 '20 at 20:18
  • I have tried something like below: but I am not successful in getting printed A1: >>> d = {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1 } >>> for x in sorted(list(d)[0:3]): ... print(x) ... A2 A3 A6 – VijZ Sep 21 '20 at 20:27
  • @Vijz you are sorting only the first three, you have to slice after sorting – Yannick Funk Sep 21 '20 at 20:30
  • @Vijz you can slice the result_list in my answer with `result_list[:3]` to get the top three, if you think my answer is satisfactory, please accept it as best answer – Yannick Funk Sep 21 '20 at 20:31
  • Thanks a lot for your helping hand. Question is not straight forward as it appeared to be. That's the reason I had provided expected output. 1. If all the values are same, then print all the relevant keys. 2. If two or more keys share the same value appear which appeared in the top 3 list say like top 2nd, then print the top 3rd key by value – VijZ Sep 21 '20 at 20:38
  • @VijZ I think you gained enough input now to solve it yourself, one more tip might be that you can get both keys and values with d.items() – Yannick Funk Sep 21 '20 at 20:39
  • I tried something like below: but not getting expected result: >>> import operator >>> d = {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1 } >>> sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1),reverse=True)) >>> for x in sorted(list(sorted_d)[0:]): ... print(x) ... A1 A10 A2 A3 A5 A6 >>> – VijZ Sep 21 '20 at 21:20
  • @VijZ The following code should work. The idea is to use Python's `set()` to obtain top 3 values without duplicates unlike in a `list` and then to use list comprehension by iterating over keys of the original `dict` to find if the mapped value is in the top 3. `top_values = sorted(set(d.values()), reverse=True)` `for k in sorted([k for k,v in d.items() if v in top_values[:3]]):` `print(k)` – so2 Jan 01 '22 at 18:26
0

In python 3.6+ you can first sort the dict by value

sorted_d = {k: v for k, v in sorted(d.items(), key=lambda item: -item[1])}
print(sorted_d)
# displays {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1}

Then keep the top 3 keys and order it again by reversing the elements:

# slicing then reversing
top3_keys = [k for k in sorted_d][0:3][::-1]
print(top3_keys)
# displays ['A2', 'A3', 'A6']
Pierre-Selim
  • 131
  • 2
  • Thank you. Since, A2 and A3 have the same value. I would like to have 'A1' as well in the list. If they don't share the same value, your code makes sense – VijZ Sep 21 '20 at 21:02
  • it really makes things complicated but I guess you could count the number of values before reaching the 3rd different values and remplace this in the slicing. – Pierre-Selim Sep 21 '20 at 21:08
  • I tried something like below: but not getting expected result: >>> import operator >>> d = {'A6': 5, 'A3': 4, 'A2': 4, 'A1': 3, 'A5': 2, 'A10': 1 } >>> sorted_d = dict(sorted(d.items(), key=operator.itemgetter(1),reverse=True)) >>> for x in sorted(list(sorted_d)[0:]): ... print(x) ... A1 A10 A2 A3 A5 A6 >>> – VijZ Sep 21 '20 at 21:20