What are python's equivalents of std::lower_bound and std::upper_bound C++ algorithms?

左心房为你撑大大i 提交于 2020-06-24 06:08:27

问题


Does python provide functions for performing binary search on sorted lists, analogous to the std::lower_bound and std::upper_bound algorithms of the C++ Standard Library?


回答1:


Those functions are located in the bisect module:

  • bisect.bisect_left(a, x, lo=0, hi=len(a)) is the analog of std::lower_bound().

  • bisect.bisect_right(a, x, lo=0, hi=len(a)) is the analog of std::upper_bound().

Note: there is also a function bisect() which is an alias for bisect_right().



来源:https://stackoverflow.com/questions/37873954/what-are-pythons-equivalents-of-stdlower-bound-and-stdupper-bound-c-algor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!