list - How to Sort Python Objects -


i have nested list contains different objects, they're duplicate pairs of objects in nested list , i'm trying remove them keep getting

typeerror: unorderable types: practice() < practice()

i know error caused me trying work objects rather integers don't know how else remove duplicates here tried

class practice:     id = none      def __init__(self,id):         self.id = id  = 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() item in x:     if sorted(item) not in unique_list:         unique_list.append(sorted(item))  print(unique_list) 

if want compare objects 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() item in x:     if sorted(item) not in unique_list:         unique_list.append(sorted(item))  print(unique_list) [[<__main__.practice object @ 0x7fe87e717c88>, <__main__.practice object @ 0x7fe87e717cc0>],  [<__main__.practice object @ 0x7fe86f5f79e8>, <__main__.practice object @ 0x7fe86f589278>],  [<__main__.practice object @ 0x7fe86f589be0>, <__main__.practice object @ 0x7fe86f589c18>]] 

depending on functionality want implement rich comparison ordering methods can use functools.total_ordering, need define 1 of methods , take care of 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 class defining 1 or more rich comparison ordering methods, class decorator supplies rest. simplifies effort involved in specifying of possible rich comparison operations:

the class must define 1 of __lt__(), __le__(), __gt__(), or __ge__(). in addition, class should supply __eq__() method.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -