How does a Python custom comparator work?

前端 未结 4 1522
甜味超标
甜味超标 2021-01-06 19:52

I have the following Python dict:

[(2, [3, 4, 5]), (3, [1, 0, 0, 0, 1]), (4, [-1]), (10, [1, 2, 3])]

Now I want to sort them on the basis o

相关标签:
4条回答
  • 2021-01-06 20:29

    The number and which comparisons are done is not documented and in fact, it can freely change from different implementations. The only guarantee is that if the comparison function makes sense the method will sort the list.

    CPython uses the Timsort algorithm to sort lists, so what you see is the order in which that algorithm is performing the comparisons (if I'm not mistaken for very short lists Timsort just uses insertion sort)

    Python is not keeping track of "keys". It just calls your comparison function every time a comparison is made. So your function can be called many more than len(items) times.

    If you want to use keys you should use the key argument. In fact you could do:

    items.sort(key=lambda x: sum(x[1]))
    

    This will create the keys and then sort using the usual comparison operator on the keys. This is guaranteed to call the function passed by key only len(items) times.


    Given that your list is:

    [a,b,c,d]
    

    The sequence of comparisons you are seeing is:

    b < a   # -1  true   --> [b, a, c, d]
    c < b   # -1  true   --> [c, b, a, d]
    d < c   # 1   false
    d < b   # 1   false
    d < a   # -1  true   --> [c, b, d, a]
    
    0 讨论(0)
  • 2021-01-06 20:29

    how the comparator is working

    This is well documented:

    Compare the two objects x and y and return an integer according to the outcome. The return value is negative if x < y, zero if x == y and strictly positive if x > y.

    Instead of calling the cmp function you could have written:

    sum_a=sum(a[1])
    sum_b=sum(b[1])
    if sum_a < sum_b: 
       return -1
    elif sum_a == sum_b:
       return 0
    else:
       return 1
    

    which two values are being passed

    From your print statements you can see the two values that are passed. Let's look at the first iteration:

    ((3, [1, 0, 0, 0, 1]), (2, [3, 4, 5]))

    What you are printing here is a tuple (a, b), so the actual values passed into your comparison functions are

    a = (3, [1, 0, 0, 0, 1])
    b = (2, [3, 4, 5]))
    

    By means of your function, you then compare the sum of the two lists in each tuple, which you denote sum_a and sum_b in your code.

    and how many such comparisons would happen?

    I guess what you are really asking: How does the sort work, by just calling a single function?

    The short answer is: it uses the Timsort algorithm, and it calls the comparison function O(n * log n) times (note that the actual number of calls is c * n * log n, where c > 0).

    To understand what is happening, picture yourself sorting a list of values, say v = [4,2,6,3]. If you go about this systematically, you might do this:

    1. start at the first value, at index i = 0
    2. compare v[i] with v[i+1]
    3. If v[i+1] < v[i], swap them
    4. increase i, repeat from 2 until i == len(v) - 2
    5. start at 1 until no further swaps occurred

    So you get, i =

    0: 2 < 4 => [2, 4, 6, 3] (swap)
    1: 6 < 4 => [2, 4, 6, 3] (no swap)
    2: 3 < 6 => [2, 4, 3, 6] (swap)
    

    Start again:

    0: 4 < 2 => [2, 4, 3, 6] (no swap)
    1: 3 < 4 => [2, 3, 4, 6] (swap)
    2: 6 < 4 => [2, 3, 4, 6] (no swap)
    

    Start again - there will be no further swaps, so stop. Your list is sorted. In this example we have run through the list 3 times, and there were 3 * 3 = 9 comparisons.

    Obviously this is not very efficient -- the sort() method only calls your comparator function 5 times. The reason is that it employs a more efficient sort algorithm than the simple one explained above.

    Also the behavior seems to be very random.

    Note that the sequence of values passed to your comparator function is not, in general, defined. However, the sort function does all the necessary comparisons between any two values of the iterable it receives.

    Is it creating a sorted list of keys internally where it keeps track of each comparison made?

    No, it is not keeping a list of keys internally. Rather the sorting algorithm essentially iterates over the list you give it. In fact it builds subsets of lists to avoid doing too many comparisons - there is a nice visualization of how the sorting algorithm works at Visualising Sorting Algorithms: Python's timsort by Aldo Cortesi

    0 讨论(0)
  • 2021-01-06 20:44

    First, the cmp() function:

    cmp(...)
        cmp(x, y) -> integer
        Return negative if x<y, zero if x==y, positive if x>y.
    

    You are using this line: items.sort(myComparator) which is equivalent to saying: items.sort(-1) or items.sort(0) or items.sort(1)

    Since you want to sort based on the sum of each tuples list, you could do this:

    mylist = [(2, [3, 4, 5]), (3, [1, 0, 0, 0, 1]), (4, [-1]), (10, [1, 2, 3])]
    sorted(mylist, key=lambda pair: sum(pair[1]))
    

    What this is doing is, I think, exactly what you wanted. Sorting mylist based on the sum() of each tuples list

    0 讨论(0)
  • 2021-01-06 20:45

    Basically, for the simple list such as [2, 4, 6, 3, 1] and the complex list you provided, the sorting algorithms are the same.

    The only differences are the complexity of elements in the list and the comparing scheme that how to compare any tow elements (e.g. myComparator you provided).

    There is a good description for Python Sorting: https://wiki.python.org/moin/HowTo/Sorting

    0 讨论(0)
提交回复
热议问题