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)
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With