When are bisect_left and bisect_right not equal?

后端 未结 5 1330
半阙折子戏
半阙折子戏 2021-02-02 09:17

In my understanding, bisect_left and bisect_right are two different ways of doing the same thing: bisection, one coming from the left and the other coming from the right. Thus,

5条回答
  •  青春惊慌失措
    2021-02-02 09:24

    bisect.bisect_left returns the leftmost place in the sorted list to insert the given element. bisect.bisect_right returns the rightmost place in the sorted list to insert the given element.

    An alternative question is when are they equivalent? By answering this, the answer to your question becomes clear.

    They are equivalent when the the element to be inserted is not present in the list. Hence, they are not equivalent when the element to be inserted is in the list.

提交回复
热议问题