Sorting a dictionary of tuples in Python

耗尽温柔 提交于 2020-01-01 08:57:51

问题


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


回答1:


Something like this?

>>> a = {'a': (1, 2, 3), 'b': (3, 2, 1)}
>>> b = a.items()
>>> b
[('a', (1, 2, 3)), ('b', (3, 2, 1))]
>>> b.sort(key=lambda x:x[1][2])  # sorting by the third item in the tuple
>>> b
[('b', (3, 2, 1)), ('a', (1, 2, 3))]



回答2:


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)


来源:https://stackoverflow.com/questions/7349646/sorting-a-dictionary-of-tuples-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!