alain wrote: > I have a problem I wonder if it has been solved before. > I have a dictionnary and I want the values in the dictionnary to be > annotated with the rank that would be obtained by sorting the values > > def annotate_with_rank(my_dict): > .... > return my_annotated_dict > > In other words, any value a_value would become a 2-tuple > (a_value,rank_of_a_value) > > I seek an elegant solution. > > Alain > >>> d = dict(a=10, b=5, c=8, d=12)
>>> dict((k, (v, rank)) for rank, (v, k) in ... enumerate(sorted((v, k) for k, v in d.items()))) {'a': (10, 2), 'c': (8, 1), 'b': (5, 0), 'd': (12, 3)} >>> # sort by value, then by key since (v,k) must be unique Michael -- http://mail.python.org/mailman/listinfo/python-list