August 2018
Intermediate to advanced
366 pages
10h 14m
English
So far, we've only sorted and looked up the entries themselves, but in many cases you will have complex objects where you are interested in sorting and searching for a specific property of an object.
For example, you might have a list of people and you want to sort by their names:
class Person:
def __init__(self, name, surname):
self.name = name
self.surname = surname
def __repr__(self):
return '<Person: %s %s>' % (self.name, self.surname)
people = [Person('Derek', 'Zoolander'),
Person('Alex', 'Zanardi'),
Person('Vito', 'Corleone')
Person('Mario', 'Rossi')]
Sorting those people by name can be done by relying on the key argument of the sorted function, which specifies a callable that should return the value for which the entry ...