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

前端 未结 4 1387
旧时难觅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 15:01

    As an addition to the nice suggestions, I'd like to add my own answer which works with floats (as I just figured it out)

    bisect.bisect_left(test_array,(min_value+abs(min_value)*sys.float_info.epsilon),))
    

    would work (whether min_value is positive or not). epsilon multiplied by min_value is guaranteed to be meaningful when added to min_value (it is not absorbed/cancelled). So it's the closest greater value to min_value and bisect will work with that.

    If you have only integers that will still be faster & clearer:

    bisect.bisect_left(test_array,(min_value+1,))
    

提交回复
热议问题