I have a list of tuples. Each tuple consists of another tuple and an index. I want to sort the list depending on the difference between the values of the inner tuple. Or, in another notation:
[((float, float), int), ...]
The two floats are what I want to use in the sort key.
I wrote this:
from math import fabs
def sort_for_avg(probabilities): # 'probabilites' is a list of float-pairs
return sorted(zip(probabilites, range(len(probabilites))),
key=lambda entry: fabs(entry[0][0]-entry[0][1]))
I tried working with itemgetter, but I didn't get the hang of it for something that isn't just sorting with a simple lookup. The 'sorted' list goes all over the place, seemingly randomly.
As the probability table can get quite big, I wanted my solution to not copy any values, which is why this is such a complicated one-liner. If someone knew how to do the same in a couple of lines with generators, I would be happy, too.
[((float, float), int), ...]