The task is: there is a list of two different values, here 0 and 1(in a real problem, string values). You need to make a list of the number of zeros going in a row (something like a "combo" of zeros). So if data is: [0, 1, 1, 0, 0, 0, 1, 0, 0,] resul must be: [1, 3, 2] (the number of "combos" of zeros) My code using a loop:
data = [0, 1, 1, 0, 0, 0, 1, 0, 0,]
zeros_combo = []
count_zeros = 0
for value in data:
if value == 0:
count_zeros += 1
else:
# reset the counter, here value == 1
if count_zeros > 0:
zeros_combo.append(count_zeros)
count_zeros = 0
# Adding the last values
if count_zeros > 0:
zeros_combo.append(count_zeros)
print(zeros_combo) # result: [1, 3, 2]
Is there any way to remake this code without using a loop to speed up? Maybe, with pandas, numpy, or other tools.. Vectorized functions.. Cause with the iteration of elements in the loop and comparison, it turns out slowly.
I tried different ways, almost all turned out to be slower.
res = [v for v in map(len, ''.join(map(str, data)).split('1')) if v != 0]
res = list(filter(lambda v: v != 0, map(len, ''.join(map(str, data)).split('1'))))
import re
res = list(map(len, re.findall(r'0+', ''.join(map(str, data)))))
from itertools import groupby
res = [len(list(g)) for k, g in groupby(data, lambda x: x==0) if k]
Super slow:
import pandas as pd
s = pd.Series(data)
res = s[s==0].groupby(s.diff().ne(0).cumsum()).agg(len).values