使用@WarrenWeckesser在此处
np.diff给出的和方法来查找数组中的零游程:
import numpy as npdef zero_runs(a): # from link iszero = np.concatenate(([0], np.equal(a, 0).view(np.int8), [0])) absdiff = np.abs(np.diff(iszero)) ranges = np.where(absdiff == 1)[0].reshape(-1, 2) return rangesa = [34,2,3,22,22,22,22,22,22,18,90,5,-55,-19,22,6,6,6,6,6,6,6,6,23,53,1,5,-42,82]zero_runs(np.diff(a))Out[87]: array([[ 3, 8], [15, 22]], dtype=int32)
然后可以根据运行开始和结束之间的差异来过滤:
runs = zero_runs(np.diff(a))runs[runs[:, 1]-runs[:, 0]>5] # runs of 7 or more, to illustrate filterOut[96]: array([[15, 22]], dtype=int32)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)