I have a list of tuples that I want to sort based on a fraction of two of the elements in the tuple. The problem I'm running into, is that Python's sort() method for lists appears to not work on fractions.
Before: A: [(4, 1, 0), (4, 1, 1), (1, 1, 2), (4, 7, 3), (2, 4, 4), (1, 4, 5), (3, 1, 6), (2, 1, 7), (3, 9, 8), (1, 6, 9)]
After: A: [(1, 6, 9), (1, 4, 5), (3, 9, 8), (2, 4, 4), (1, 1, 2), (4, 7, 3), (4, 1, 0), (4, 1, 1), (3, 1, 6), (2, 1, 7)]
The code that's doing the sorting:
print("A: " + str(A))
A.sort(key=lambda tup: long(tup[1]/tup[0]), reverse=True)
print("A: " + str(A))
I think that according to the above code, the element (1,1,2), and the element (4,7,3) should be switched, because 7/4 > 1/1. I'm also pretty sure that all of the fractions at the end are wrong, which makes me think it's just doing integer division, and rounding. I thought casting it to a long might fix this, but it did not.