0

I have a dictionary with string-type keys and float-type values. I sort the dictionary by the value in descending order and wang to print the v=keys and values;

sum_count = sum(character_dist.values())
character_dist_2 = {k: v/sum_count for k,v in character_dist.items()}

sorted_dict = {}
sorted_keys = sorted(character_dist_2, key=character_dist_2.get,reverse=True)  # [1, 3, 2]

for w in sorted_keys:
    sorted_dict[w] = character_dist_2[w]

the sample for keys and values:

,_and_ 0.0029020624
_that_ 0.0020209420
_with_ 0.0016136799
n_the_ 0.0014651189
_and_t 0.0013601016
d_the_ 0.0011884881
_of_th 0.0011859267
and_th 0.0011167690
nd_the 0.0011065234
_they_ 0.0010424884
of_the 0.0010143131
e_was_ 0.0010143131
_and_s 0.0009886991
t_the_ 0.0009554010
_the_s 0.0009528396
f_the_ 0.0009374712
_in_th 0.0008964888
in_the 0.0008426995

I want to print 20 samples from the dictionary, but I don't know how to code if several keys have the same values, keys are additionally sorted lexicographically.

L = 20
original_N = L
for key, value in sorted_dict.items():

  # this is the part I don't know
  if L !=0:
    print("% s %.10f" %(key, value))
    L = L-1

Thanks

  • refer this: https://stackoverflow.com/questions/10664856/make-a-dictionary-with-duplicate-keys-in-python – Rima Mar 11 '21 at 21:56

1 Answers1

1

You can sort the dictionary keys and values together and use that to form the new dictionary directly:

sorted_dict = dict(sorted(character_dist_2.items(),key=lambda kv:(-kv[1],kv[0])))

The sort key here is a tuple combining the negative value with the key. This will make values sort in descending order with the keys in ascending order for identical values.

Example:

character_dist   = {"gg": 1, "hh": 2, "tt": 2, "qq":3, "ww": 4, "oo":5, "pp": 6}
sum_count        = sum(character_dist.values())
character_dist_2 = {k: v/sum_count for k,v in character_dist.items()}
sorted_dict = dict(sorted(character_dist_2.items(),key=lambda kv:(-kv[1],kv[0])))

print(sorted_dict)

{'pp': 0.2608695652173913,   # values in descending order
 'oo': 0.21739130434782608, 
 'ww': 0.17391304347826086, 
 'qq': 0.13043478260869565, 
 'hh': 0.08695652173913043,  # same value,  
 'tt': 0.08695652173913043,  # lexicographic order
 'gg': 0.043478260869565216}
Alain T.
  • 40,517
  • 4
  • 31
  • 51
  • Thank, but it did not solve the issue. As for printing the key and values, if I have {"gg": 1, "hh": 2, "tt": 2, "qq":3, "ww": 4, "oo":5, "pp": 6}, and I will print 5 of them into descending order, the result I have is: pp: 6; oo:5; ww: 4; qq:3; tt: 2; hh: 2 , I used the "sorted_dict" you provided but still unclear how to fix this problem. – KnowTooLess Mar 12 '21 at 03:33
  • I added an example to illustrate how to copy/paste the solution – Alain T. Mar 12 '21 at 13:31
  • Thanks, it's helpful – KnowTooLess Mar 12 '21 at 21:18