bisect_left 和 bisect_right 何时不相等? [英] When are bisect_left and bisect_right not equal?

查看:43
本文介绍了bisect_left 和 bisect_right 何时不相等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的理解中,bisect_left 和 bisect_right 是做同一件事的两种不同方式:二等分,一种来自左侧,另一种来自右侧.因此,它们具有相同的结果.在什么情况下这两个不相等,即假设列表和正在搜索的值相同,它们什么时候会返回不同的结果?

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, it follows that they have the same result. Under what circumstances are these two not equal, i.e. when will they return different results, assuming the list and the value that is being searched are the same?

推荐答案

bisect.bisect_left 返回排序列表中最左边的位置以插入给定元素.bisect.bisect_right 返回排序列表中最右边的位置以插入给定元素.

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.

这篇关于bisect_left 和 bisect_right 何时不相等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆