using bisect on list of tuples but compare using first value only

前端 未结 4 1320
旧时难觅i
旧时难觅i 2021-01-18 14:15

I read that question about how to use bisect on a list of tuples, and I used that information to answer that question. It works, but I\'d like a more generic so

4条回答
  •  礼貌的吻别
    2021-01-18 14:47

    This is a (quick'n'dirty) bisect_left implementation that allows an arbitrary key function:

    def bisect(lst, value, key=None):
        if key is None:
            key = lambda x: x
        def bis(lo, hi=len(lst)):
            while lo < hi:
                mid = (lo + hi) // 2
                if key(lst[mid]) < value:
                    lo = mid + 1
                else:
                    hi = mid
            return lo
        return bis(0)
    
    > from _operator import itemgetter
    > test_array = [(1, 2), (3, 4), (4, 3), (5.2, 6), (5.2, 7000), (5.3, 8), (9, 10)]
    > print(bisect(test_array, 5, key=itemgetter(0)))
    3
    

    This keeps the O(log_N) performance up since it does not assemble a new list of keys. The implementation of binary search is widely available, but this was taken straight from the bisect_left source. It should also be noted that the list needs to be sorted with regard to the same key function.

提交回复
热议问题