【发布时间】:2016-10-18 20:12:53
【问题描述】:
python 是否提供对排序列表执行二进制搜索的函数,类似于 C++ 标准库的 std::lower_bound 和 std::upper_bound 算法?
【问题讨论】:
标签: python binary-search
python 是否提供对排序列表执行二进制搜索的函数,类似于 C++ 标准库的 std::lower_bound 和 std::upper_bound 算法?
【问题讨论】:
标签: python binary-search
这些函数位于bisect 模块中:
bisect.bisect_left(a, x, lo=0, hi=len( a)) 是std::lower_bound() 的类似物。
bisect.bisect_right(a, x, lo=0, hi=len( a)) 是std::upper_bound() 的类似物。
注意:还有一个函数bisect(),它是bisect_right()的别名。
【讨论】: