Estilus - 10 months ago 78

Python Question

I have an arbitrary array with only binary values- say:

`a = np.array([1,0,0,0,0,0,1,1,1,1,1,1,1,1,0,0,1,1,0,])`

What would be the most efficient way to count average length of sequences of 1s array? Eg. In this example, it would be (1 + 8 + 2)/3.

Answer

I'm not sure easiest, but one alternative is

```
np.mean([len(list(v)) for k,v in itertools.groupby(a) if k])
3.6666666666666665
```

**Explanation**
`groupby`

groups adjacent equal values together, we filter only ones (`if k`

), the list comprehension `[...]`

creates the list of the lengths of sub sequences of ones, i.e. `[1,8,2]`

, and `mean`

computes the average value.

Source (Stackoverflow)