简体   繁体   English

如何排序Python对象

[英]How to Sort Python Objects

I have a nested list which contains different objects, they're duplicate pairs of objects in the nested list and i'm trying to remove them but i keep getting a 我有一个嵌套列表,其中包含不同的对象,它们是嵌套列表中的重复对象对象,我正在尝试删除它们但我一直在

TypeError: unorderable types: practice() < practice()

I know this error is caused by me trying to work with objects rather than integers but i don't know how else to remove the duplicates here is what i tried 我知道这个错误是由我尝试使用对象而不是整数引起但我不知道如何删除重复项这里是我试过的

class practice:
    id = None

    def __init__(self,id):
        self.id = id

a = practice('a')
b = practice('b')
c = practice('c')
d = practice('d')
e = practice('e')
f = practice('f')

x = [[a,b],[c,d],[a,b],[e,f],[a,b]]

unique_list = list()
for item in x:
    if sorted(item) not in unique_list:
        unique_list.append(sorted(item))

print(unique_list)

If you want to compare the objects by the id: 如果要按id比较对象:

class practice:
    id = None

    def __init__(self,id):
        self.id = id

    def __lt__(self, other):
        return other.id > self.id

    def __gt__(self, other):
        return self.id > other.id

unique_list = list()
for item in x:
    if sorted(item) not in unique_list:
        unique_list.append(sorted(item))

print(unique_list)
[[<__main__.practice object at 0x7fe87e717c88>, <__main__.practice object at 0x7fe87e717cc0>],
 [<__main__.practice object at 0x7fe86f5f79e8>, <__main__.practice object at 0x7fe86f589278>],
 [<__main__.practice object at 0x7fe86f589be0>, <__main__.practice object at 0x7fe86f589c18>]]

Depending on the functionality you want to implement all the rich comparison ordering methods you can use functools.total_ordering , you just need to define one of the methods and it will take care of the rest 根据您要实现的所有丰富的比较排序方法的功能,您可以使用functools.total_ordering ,您只需要定义其中一个方法,它将负责其余的

from functools import total_ordering
@total_ordering
class practice:
    id = None

    def __init__(self,id):
        self.id = id

    def __lt__(self, other):
        return other.id > self.id

    def __eq__(self, other):
        return self.id == other.id

Given a class defining one or more rich comparison ordering methods, this class decorator supplies the rest. 给定一个定义一个或多个丰富的比较排序方法的类,这个类装饰器提供其余的。 This simplifies the effort involved in specifying all of the possible rich comparison operations: 这简化了指定所有可能的丰富比较操作所涉及的工作:

The class must define one of __lt__() , __le__() , __gt__() , or __ge__() . 该类必须定义__lt__()__le__() __lt__()__le__() __gt__()__ge__() In addition, the class should supply an __eq__() method. 此外,该类应提供__eq__()方法。

To support sorting without explicit keys for objects in Python 3, you must implement the __lt__ special method: 要支持在Python 3中没有对象的显式键的排序,您必须实现__lt__特殊方法:

class practice:
    id = None

    def __init__(self,id):
        self.id = id

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

If you want the other operators to work, you'll have to implement their special methods as well, but for sorting __lt__ is all you need. 如果你想让其他运算符工作,你也必须实现它们的特殊方法,但是对于排序__lt__就是你所需要的。

As noted in the comments, the other way to do it is to provide an explicit key function to the sorted built-in: 如注释中所述,另一种方法是为已sorted内置提供显式键函数:

sorted(item, key=lambda x: x.id)

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

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