I have a function that looks like the following:
def f(lst, c):
scored_vals = []
for i in lst:
scored_vals += [(i.value/i.weight, i)]
scored_vals.sort()
return scored_vals
This function crashes every time it reaches scored_vals.sort()
. The argument lst
is a list comprised of Item
s, where the Item
class looks like the following:
class Item():
def __init__(self, weight=0, value=0):
self.weight = weight
self.value = value
And the argument c
is just an integer.
The error thrown is below:
scored_vals.sort()
TypeError: '<' not supported between instances of 'Item' and 'Item'
I find this very strange, because normally when you call python sort, it only compare the first item of each tuple in order to sort the list, so if you create the list yourself like below:
lst = [(201.0, Item()), (350.0, Item()), (202.0, Item()), (100.0, Item()), (500.0, Item()), (203.0, Item())]
Then do:
lst.sort()
It throws no error.