Java相当于python中的bisect
Java 中是否有与 Python 的 bisect 模块 等效的模块?使用 Python 的二分法,您可以按方向进行数组二分法。例如 bisect.bisect_left
的作用是:
找到列表中项目的正确插入点以保持排序顺序。参数 lo 和 hi 可用于指定应考虑的列表子集;默认情况下使用整个列表。
我知道我也可以通过二分搜索手动执行此操作,但我想知道是否已经有一个库或集合可以执行此操作。
Is there an equivalent in Java for Python's bisect module? With Python's bisect you can do array bisection with directions. For instance bisect.bisect_left
does:
Locate the proper insertion point for item in list to maintain sorted order. The parameters lo and hi may be used to specify a subset of the list which should be considered; by default the entire list is used.
I know I can do this manually with a binary search too, but I was wondering if there is already a library or collection doing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您有两个选择:
java.util.Arrays.binarySearch
java.util.Collections.binarySearch
位于列表
可比较
和比较器
重载)。List.subList(int fromIndex, int toIndex)
搜索列表的一部分You have two options:
java.util.Arrays.binarySearch
on arraysjava.util.Collections.binarySearch
onList
Comparable
andComparator
overloads).List.subList(int fromIndex, int toIndex)
to search portion of a list到目前为止(Java 8),它仍然缺失,所以您仍然必须自己制作。这是我的:
经过测试(X 是我存储我打算重用的静态方法的类):
To this date (Java 8), this is still missing, so you must still make your own. Here's mine:
Tested with (X being the class where I store static methods that I intend to reuse):
为了完整起见,这里有一个小 java 函数,它将 Arrays.binarySearch 的输出转换为接近 bisect_left 的输出。我显然错过了一些东西,但这足以解决简单的情况。
Just for completeness, here's a little java function that turns the output from
Arrays.binarySearch
into something close to the output frombisect_left
. I'm obviously missing things, but this does the job for the simple case.为什么不快速移植经过尝试和测试 Python 代码本身?例如,以下是
bisect_right
的 Java 端口:Why not do a quick port of the tried and tested Python code itself? For example, here's a Java port for
bisect_right
:基于
java.util.Arrays.binarySearch
文档这里我使用
long[]
数组的示例,但可以调整代码以利用任何受支持的类型。
注意:java API 的限制,来自 javadoc 的以下句子:
事实上,我已经使用不同元素的排序数组对此进行了测试。
我的用例是范围分组,其中
arr
是一组不同的时间戳,指示间隔的开始时间。Based on the
java.util.Arrays.binarySearch
documentationHere I use the example for a
long[]
array,but one can adapt the code to utilize any of the supported types.
Note: Limitation on the java API, by the following sentence from javadoc:
Indeed, I've tested that with sorted array of distinct elements.
My use-case was for range grouping, where
arr
an array of distinct timestamps that indicate the start time of an interval.你需要自己定义,这是我的:
bisect.bisect_left
bisect.bisect_right
You need to define on your own, here's mine:
bisect.bisect_left
bisect.bisect_right
源自@Profiterole的答案,这里是一个通用变体,它使用 int->boolean 函数而不是数组。它找到谓词发生变化的第一个索引。
例如,要查找数组中的插入点,该函数会将插入的值与数组的值进行比较:
Derived from @Profiterole's answer, here is a generalized variant that works with an int->boolean function instead of an array. It finds the first index where the predicate changes.
For example, to find the insertion point in an array, the function compares the value inserted with the values of the array: