Olga - 1 year ago 167

Python Question

I use Python with

`numpy`

I have a numpy array

`b`

`b = np.array([True,True,True,False,False,True,True,False,True,True,True,True,False])`

I need to find the first and last index where

`b`

`True`

For this exsample:

`out_index: [0,2]`

[5,6]

[8,11]

Can someone please suggest,

`out_index`

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

```
b = np.array([True,True,True,False,False,True,True,False,True,True,True,True,False])
idx = np.argwhere(np.diff(np.r_[False, b, False])).reshape(-1, 2)
idx[:, 1] -= 1
print idx
```

output:

```
[[ 0 2]
[ 5 6]
[ 8 11]]
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**