[Tutor] custom comparator with ordered list
anish singh
anish198519851985 at gmail.com
Mon Jun 26 04:48:40 EDT 2017
I need a custom comparator.
dictionary = {a:[b,c], d:[e,f]}
If both 'b' and 'e' belong to the same bin
then it should be compared based on 'c' and 'f'.
However, I want to also represent the result of the
sorted operation in a ordered dictionary as order is
important.
My custom comparator is something like this:
''' x and y is a list of two elements each'''
def cmpr(x, y):
r = 3
if x[0]//r != y[0]//r:
return x[0]//r < y[0]//r
return x[1] < y[1]
Please note it is not exactly comparing the first elements
of the value but checking if they belong to the same bin
and they do then it checks the second element as as shown
above.
Example:
{0:[0, 8], 1:[2, 5], 2:[2, 11], 3:[16, 17], 4:[13, 14], 5:[1, 17], 6:[17,
17] }
output should be:
{1:[2, 5], 0:[0, 8], 2:[2, 11], 5:[1, 17], 4:[13, 14], 3:[16, 17], 6:[17,
17] }
http://ideone.com/lXBdr2
More information about the Tutor
mailing list