如何在NumPy数组中查找连续元素的组
问题描述:
我必须对NumPy数组中的连续元素进行聚类.考虑以下示例
I have to cluster the consecutive elements from a NumPy array. Considering the following example
a = [ 0, 47, 48, 49, 50, 97, 98, 99]
输出应为元组列表,如下所示:
The output should be a list of tuples as follows
[(0), (47, 48, 49, 50), (97, 98, 99)]
在这里,元素之间的区别只是一个.如果也可以将差异指定为限制或硬编码数字,那就太好了.
Here the difference is just one between the elements. It will be great if the difference can also be specified as a limit or a hardcoded number.
答
下面是一个可能有用的功能:
Here's a lil func that might help:
def group_consecutives(vals, step=1):
"""Return list of consecutive lists of numbers from vals (number list)."""
run = []
result = [run]
expect = None
for v in vals:
if (v == expect) or (expect is None):
run.append(v)
else:
run = [v]
result.append(run)
expect = v + step
return result
>>> group_consecutives(a)
[[0], [47, 48, 49, 50], [97, 98, 99]]
>>> group_consecutives(a, step=47)
[[0, 47], [48], [49], [50, 97], [98], [99]]