简体   繁体   English

如何根据对象的属性对对象列表进行排序?

[英]How to sort a list of objects based on an attribute of the objects?

I have a list of Python objects that I want to sort by a specific attribute of each object:我有一个 Python 对象的列表,我想按每个 object 的特定属性对其进行排序:

>>> ut
[Tag(name="toe", count=10), Tag(name="leg", count=2), ...]

How do I sort the list by .count in descending order?如何按.count降序对列表进行排序?

# To sort the list in place...
ut.sort(key=lambda x: x.count, reverse=True)

# To return a new list, use the sorted() built-in function...
newlist = sorted(ut, key=lambda x: x.count, reverse=True)

More on sorting by keys .更多关于按键排序的信息。

A way that can be fastest, especially if your list has a lot of records, is to use operator.attrgetter("count") .一种最快的方法是使用operator.attrgetter("count") ,特别是如果您的列表有很多记录。 However, this might run on an pre-operator version of Python, so it would be nice to have a fallback mechanism.但是,这可能会在 Python 的预操作员版本上运行,因此最好有一个回退机制。 You might want to do the following, then:然后,您可能想要执行以下操作:

try: import operator
except ImportError: keyfun= lambda x: x.count # use a lambda if no operator module
else: keyfun= operator.attrgetter("count") # use operator since it's faster than lambda

ut.sort(key=keyfun, reverse=True) # sort in-place

Readers should notice that the key= method:读者应该注意到 key= 方法:

ut.sort(key=lambda x: x.count, reverse=True)

is many times faster than adding rich comparison operators to the objects.比向对象添加丰富的比较运算符要快很多倍。 I was surprised to read this (page 485 of "Python in a Nutshell").我很惊讶地读到这个(“Python in a Nutshell”的第 485 页)。 You can confirm this by running tests on this little program:您可以通过在这个小程序上运行测试来确认这一点:

#!/usr/bin/env python
import random

class C:
    def __init__(self,count):
        self.count = count

    def __cmp__(self,other):
        return cmp(self.count,other.count)

longList = [C(random.random()) for i in xrange(1000000)] #about 6.1 secs
longList2 = longList[:]

longList.sort() #about 52 - 6.1 = 46 secs
longList2.sort(key = lambda c: c.count) #about 9 - 6.1 = 3 secs

My, very minimal, tests show the first sort is more than 10 times slower, but the book says it is only about 5 times slower in general.我的,非常小的测试显示第一种慢了 10 倍以上,但书上说它通常只慢 5 倍。 The reason they say is due to the highly optimizes sort algorithm used in python ( timsort ).他们说的原因是由于 python ( timsort ) 中使用的高度优化的排序算法。

Still, its very odd that .sort(lambda) is faster than plain old .sort().尽管如此,.sort(lambda) 比普通的旧 .sort() 更快,这很奇怪。 I hope they fix that.我希望他们能解决这个问题。

Object-oriented approach面向对象的方法

It's good practice to make object sorting logic, if applicable, a property of the class rather than incorporated in each instance the ordering is required.如果适用,最好将对象排序逻辑作为类的属性,而不是在需要排序的每个实例中合并。

This ensures consistency and removes the need for boilerplate code.这确保了一致性并消除了对样板代码的需求。

At a minimum, you should specify __eq__ and __lt__ operations for this to work.至少,您应该指定__eq____lt__操作才能使其工作。 Then just use sorted(list_of_objects) .然后只需使用sorted(list_of_objects)

class Card(object):

    def __init__(self, rank, suit):
        self.rank = rank
        self.suit = suit

    def __eq__(self, other):
        return self.rank == other.rank and self.suit == other.suit

    def __lt__(self, other):
        return self.rank < other.rank

hand = [Card(10, 'H'), Card(2, 'h'), Card(12, 'h'), Card(13, 'h'), Card(14, 'h')]
hand_order = [c.rank for c in hand]  # [10, 2, 12, 13, 14]

hand_sorted = sorted(hand)
hand_sorted_order = [c.rank for c in hand_sorted]  # [2, 10, 12, 13, 14]
from operator import attrgetter
ut.sort(key = attrgetter('count'), reverse = True)

It looks much like a list of Django ORM model instances.它看起来很像 Django ORM 模型实例的列表。

Why not sort them on query like this:为什么不在这样的查询中对它们进行排序:

ut = Tag.objects.order_by('-count')

Add rich comparison operators to the object class, then use sort() method of the list.将丰富的比较运算符添加到对象类中,然后使用列表的 sort() 方法。
See rich comparison in python . 在 python 中查看丰富的比较


Update : Although this method would work, I think solution from Triptych is better suited to your case because way simpler.更新:虽然这种方法可行,但我认为 Triptych 的解决方案更适合您的情况,因为它更简单。

If the attribute you want to sort by is a property , then you can avoid importing operator.attrgetter and use the property'sfget method instead.如果要排序的属性是property ,则可以避免导入operator.attrgetter并改用该属性的fget方法。

For example, for a class Circle with a property radius we could sort a list of circles by radii as follows:例如,对于具有属性radius的类Circle ,我们可以按半径对circles列表进行排序,如下所示:

result = sorted(circles, key=Circle.radius.fget)

This is not the most well-known feature but often saves me a line with the import.这不是最知名的功能,但经常为我节省了导入的一行。

Also if someone wants to sort list that contains strings and numbers for eg此外,如果有人想要对包含字符串和数字的列表进行排序,例如

 eglist=[
     "some0thing3",
     "some0thing2",
     "some1thing2",
     "some1thing0",
     "some3thing10",
     "some3thing2",
     "some1thing1",
     "some0thing1"]

Then here is the code for that:那么这里是代码:

import re

def atoi(text):
    return int(text) if text.isdigit() else text

def natural_keys(text):
    return [ atoi(c) for c in re.split(r'(\d+)', text) ]

eglist=[
         "some0thing3",
         "some0thing2",
         "some1thing2",
         "some1thing0",
         "some3thing10",
         "some3thing2",
         "some1thing1",
         "some0thing1"
]

eglist.sort(key=natural_keys)
print(eglist)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM