467,162 Members | 944 Online

dictionary: sorting the values preserving the order

 Hi, For a particular problem of mine, I want to sort pairs by its value. Eg: Input: A, 4 B, 5 C, 1 D, 2 E, 3 I would like the output to be: C D E A B i.e. I would like to get the keys in the sorted order of values. I did google around a little bit. One solution to a similar problem suggested is: # Courtesy: http://aspn.activestate.com/ASPN/Pyt...k/Recipe/52306 def sortedDictValues3(adict): keys = adict.keys() keys.sort() return map(adict.get, keys) This gets a list sorted by the keys. How would I get a revised dictionary sorted by its values. Jul 18 '05 #1
• viewed: 1826
Share:
5 Replies
 hi, assuming your key-value relationship is one-to-one then as a simple first pass you can simply initialize d1={} and for i in d.keys(): d1[d[i]] = i and pass d1 to your sortedDictValue3 function, no? thanks, Vikram On 31 Mar 2005, Rakesh wrote: Hi, For a particular problem of mine, I want to sort pairs by its value. Eg: Input: A, 4 B, 5 C, 1 D, 2 E, 3 I would like the output to be: C D E A B i.e. I would like to get the keys in the sorted order of values. I did google around a little bit. One solution to a similar problem suggested is: # Courtesy: http://aspn.activestate.com/ASPN/Pyt...k/Recipe/52306 def sortedDictValues3(adict): keys = adict.keys() keys.sort() return map(adict.get, keys) This gets a list sorted by the keys. How would I get a revised dictionary sorted by its values. Jul 18 '05 #2
 Rakesh wrote: Hi, For a particular problem of mine, I want to sort pairsby its value.Eg:Input:A, 4B, 5C, 1D, 2E, 3I would like the output to be:CDEAB the following code does that: d1 = {'a':4,'b':5,'c':1,'d':2,'e':3} i1 = [ (d1[i], i) for i in d1.keys() ] i1.sort() i1 [(1, 'c'), (2, 'd'), (3, 'e'), (4, 'a'), (5, 'b')] for each in i1: .... print each[1] c d e a b thanks, Satchit i.e. I would like to get the keys in the sorted order of values.I did google around a little bit. One solution to a similar problemsuggested is:# Courtesy:http://aspn.activestate.com/ASPN/Pyt...k/Recipe/52306def sortedDictValues3(adict): keys = adict.keys() keys.sort() return map(adict.get, keys)This gets a list sorted by the keys. How would I get a reviseddictionarysorted by its values. Jul 18 '05 #3
 On 31 Mar 2005 22:40:53 -0800, "Rakesh" wrote: Hi, For a particular problem of mine, I want to sort pairsby its value.Eg:Input:A, 4B, 5C, 1D, 2E, 3I would like the output to be:CDEABi.e. I would like to get the keys in the sorted order of values. Generally, dictionaries nearly always have two parts. The dictionary itself, and a separate list of keys to access it with. To access the dictionary in a particular order, you just need a sorted key list. Since what you want is to access by value, you need to create a second dictionary with the values as the keys. That will only work if the values never repeat. If they do, then you need to use a list and not a dictionary. This creates a second dictionary with a sorted value key list. alpha_dict = {'A':4, 'B':5, 'C':1, 'D':2, 'E':3} # Create a new dictionary with keys and values exchanged. num_dict = {} for k in alpha_dict.keys(): num_dict[ alpha_dict[k] ] = k # Get the num_dict keys and sort them. num_keys = num_dict.keys() num_keys.sort() Ron Jul 18 '05 #4
 Another alternative: d1 = {'a':4,'b':5,'c':1,'d':2,'e':3*} il=[(v,k) for k,v in d1.items()] il.sort() Jul 18 '05 #5
 "Rakesh" wrote in message news:11**********************@l41g2000cwc.googlegr oups.com... This gets a list sorted by the keys. That is all you *can* get (with the list keys being the dict values). How would I get a revised dictionary sorted by its values. You can't. A dictionary is not sorted. The print order is arbitrary and not controllable. Terry J. Reedy Jul 18 '05 #6