在 Python 中对元组字典进行排序

发布于 2024-12-03 08:38:32 字数 535 浏览 3 评论 0原文

我知道已经有很多关于 python 排序列表/字典的问题了,但我似乎找不到对我的情况有帮助的问题,而且我正在寻找最有效的解决方案,因为我要对一个相当大的数据进行排序数据集。

我的数据目前基本上是这样的:

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

我基本上正在创建一个单词列表,其中存储每个单词以及有关它的一些统计数据(n,Sigma(x),Sigma(x ^ 2))

我想对其进行排序基于特定的统计数据。到目前为止,我一直在尝试以下内容:

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

我不确定如何控制它根据何时有效地是元组的元组列表来排序的索引?我想我实际上需要嵌套两个 itemgetter 操作,但不太确定如何做到这一点。

如果我应该使用更好的数据结构,请告诉我。我是否应该创建一个小型类/结构,然后使用 lambda 函数来访问该类的成员?

非常感谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妳是的陽光 2024-12-10 08:38:32

像这样的东西吗?

>>> 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))]

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))]
木有鱼丸 2024-12-10 08:38:32

名称更容易使用并记住索引,所以我会选择一个类:

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文