[Solved] Python: Counting a list and displaying a range of the count


In[2]: data = [1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1]
In[3]: from itertools import groupby
  ...: 
  ...: dex = 1
  ...: result = []
  ...: for k, g in groupby(data):
  ...:     length = sum(1 for _ in g)
  ...:     result.append((
  ...:         (dex, dex + length - 1),
  ...:         'music' if k == 1 else 'speech'
  ...:     ))
  ...:     dex += length
  ...: 
In[4]: result
Out[4]: 
[((1, 6), 'music'),
 ((7, 12), 'speech'),
 ((13, 17), 'music'),
 ((18, 24), 'speech'),
 ((25, 26), 'music'),
 ((27, 32), 'speech'),
 ((33, 37), 'music')]

1

solved Python: Counting a list and displaying a range of the count