Given the following classes:
class Comparison():
def __init__(self, value):
self.value = value
self.rank = 0
class Comparisons(dict):
def __init__(self):
super(Comparisons, self).__init__()
def rank(self):
# Method to examine each Comparison instance and
# assign Comparison.rank based on Comparison.value
What is an efficient way for the rank()
method to examine the objects and assign a rank? For example:
comparisons = Comparisons()
# store some Comparison instances
comparisons['one'] = Comparison(10)
comparisons['two'] = Comparison(5)
comparisons['three'] = Comparison(1)
# function to rank the comparisons
comparisons.rank()
print(comparisons['one'].rank)
print(comparisons['two'].rank)
print(comparisons['three'].rank)
Returns:
3
2
1
If the rank()
method could handle ties, that would be even more beneficial.
dict
class to sort them because regular dictionaries can be sorted. Are creating a subclass for some other reason and if so, what are they?.rank()
method?