Alex Rivera | Logout

Sorting a dictionary of tuples in Python

Asked 2011-09-08T14:27:37.520
12

I know there's tonnes of questions on python sorting lists/dictionaries already, but I can't seem to find one which helps in my case, and i'm looking for the most efficient solution as I'm going to be sorting a rather large dataset.

My data basically looks like this at the moment:

a = {'a': (1, 2, 3), 'b': (3, 2, 1)}

I'm basically creating a word list in which I store each word along with some stats about it (n, Sigma(x), Sigma(x^2) )

I want to sort it based on a particular stat. So far I've been trying something along the lines of:

b = a.items()
b.sort(key = itemgetter(1), reverse=True)

I'm not sure how to control which index it is sorted based on when its effectively a list of tuples of tuples? I guess I effectively need to nest two itemgetter operations but not really sure how to do this.

If there's a better data structure I should be using instead please let me know. Should I perhaps create a small class/struct and then use a lambda function to access a member of the class?

Many Thanks

Edit
Report

1 Answer

1

Names are easier to work with and remember that indices, so I would go with a class:

class Word(object):     # don't need `object` in Python 3
    def __init__(self, word):
        self.word = word
        self.sigma = (some calculation)
        self.sigma_sq = (some other calculation)
    def __repr__(self):
        return "Word(%r)" % self.word
    def __str__(self):
        return self.word
    @property
    def sigma(self):
        return self._sigma
    @sigma.setter               # requires python 2.6+
    def sigma(self, value):
        if not value:
            raise ValueError("sigma must be ...")
        self._sigma = value

word_list = [Word('python'), Word('totally'), Word('rocks')]
word_list.sort(key=lambda w: w.sigma_sq)
answered 2011-09-08T17:24:09.720

Your Answer