- an array of folders with zero on both sides using
np.concatenate - find zero with
a == 0 - find borders using
np.diff - summarize the boundaries found with
sum - divide by two because we will find twice as much as we want
def nonzero_clusters(a): return int(np.diff(np.concatenate([[0], a, [0]]) == 0).sum() / 2)
demonstration
nonzero_clusters( [0,0,0,0,0,0,10,15,16,12,11,9,10,0,0,0,0,0,6,9,3,7,5,4,0,0,0,0,0,0,4,3,9,7,1] ) 3
nonzero_clusters([0, 1, 2, 0, 1, 2]) 2
nonzero_clusters([0, 1, 2, 0, 1, 2, 0]) 2
nonzero_clusters([1, 2, 0, 1, 2, 0, 1, 2]) 3
time
a = np.random.choice((0, 1), 100000)
the code
from itertools import groupby def div(a): m = a != 0 return (m[1:] > m[:-1]).sum() + m[0] def pir(a): return int(np.diff(np.concatenate([[0], a, [0]]) == 0).sum() / 2) def jean(a): previous = 0 count = 0 for c in a: if previous==0 and c!=0: count+=1 previous = c return count def moin(a): return len([is_true for is_true, _ in groupby(a, lambda x: x!=0) if is_true]) def user(a): return sum([1 for n in range (len (a) - 1) if not a[n] and a[n + 1]])
