Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 741
danidee Avatar asked Apr 23 '15 00:04

danidee


People also ask

Can you sort objects in Python?

Python lists have a built-in list.sort() method that modifies the list in-place. There is also a sorted() built-in function that builds a new sorted list from an iterable.

How do you sort a class object in Python?

A simple solution is to use the list. sort() function to sort a collection of objects (using some attribute) in Python. This function sorts the list in-place and produces a stable sort. It accepts two optional keyword-only arguments: key and reverse.

How do you sort two objects in Python?

sort() function. A Pythonic solution to in-place sort a list of objects using multiple attributes is to use the list. sort() function. It accepts two optional keyword-only arguments: key and reverse and produces a stable sort.

How do you sort an array of objects in Python?

Python provides a built-in method called sort to sort an array. It provides an option to specify the order of the sort. Note: Sort method doesn't return a new Array, instead it modifies the same. To sort array in descending order you can specify reverse parameter to True.


1 Answers

If you want to compare the objects by the 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

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__(). In addition, the class should supply an __eq__() method.

like image 168
Padraic Cunningham Avatar answered Sep 23 '22 22:09

Padraic Cunningham