Hrihaan - 5 months ago 23
Python Question

Count of items that occur in succession

I have the following data file.

`````` 1 3
2 6
3 7
4 6
5 8
6 4
7 5
8 9
9 7
10 2
11 3
12 5
13 3
``````

My goal is to have a count of items that are equal to or greater than 5 in column 2 which appear at least 3 times in succession. I have been able to figure out the counting part but not the succession part.

So, I want the output of this data file to be 2 as in column 2 there are 2 strings (6,7,6,8) and (5,9,7) where I have numbers that are equal to and greater than 5 appearing at least 3 times in succession.

``````import numpy as np
z=data[:,1]
count = len([i for i in z if i >= 5])
print(count)
``````

Any help would be greatly appreciated.

You could loop through the column, check and keep a running count.

``````start = 0 # Keeps track of first number >= 5
count = 0

for i in z:
if i >= 5:
start += 1
elif i < 5 and start > 2: # Checks if there were at least 3 in succession, and if the series has ended
count += 1
start = 0 # Reset start

print count
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download