Python built-in data structures
Key 1: Understanding Python's in-built data structure.
Before going in on how to use different data structures, we should take a look at the attributes of the object that are important for built-in data structures. For the default sorting to work, the object should have one of the __lt__, and __gt__ methods defined. Otherwise, we can pass a key function to the sorting method to use in getting the intermediate keys that are used to compare it, as shown in the following code:
def less_than(self, other):
return self.data <= other.data
class MyDs(object):
def __init__(self, data):
self.data = data
def __str__(self,):
return str(self.data)
__repr__ = __str__
if __name__ == '__main__':
ml = [MyDs(i) for i in range(10, 1, -1)]
try:
ml.sort()
except TypeError:
print("unable to sort by default")
for att in '__lt__', '__le__', '__gt__',...