【问题标题】:What are python's equivalents of std::lower_bound and std::upper_bound C++ algorithms?python 的 std::lower_bound 和 std::upper_bound C++ 算法等价物是什么?
【发布时间】:2016-10-18 20:12:53
【问题描述】:

python 是否提供对排序列表执行二进制搜索的函数,类似于 C++ 标准库的 std::lower_boundstd::upper_bound 算法?

【问题讨论】:

    标签: python binary-search


    【解决方案1】:

    这些函数位于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()的别名。

    【讨论】:

      猜你喜欢
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-10-27
      相关资源
      最近更新 更多