'Recover line where a sequence of consecutive positive numbers are found in a vectors

With this python code, I am able to count sequences of positive number in a column. Now I am trying to recover the line where these sequences are situated.

import numpy  as np
from itertools import groupby
data = np.loadtxt("Krin_yaoun.txt")
nlin, ncol = np.shape(data)
series = data[:, 0]
xx  = []
yy  = []
for k, g in groupby(series, key=lambda i: i > 0):
    sub_list = list(g)
 
    a = float(np.sum(sub_list))
    if a > 0: 
        xx.append(a)
        yy.append(len(sub_list))
        
        print(tuple(sub_list))


Solution 1:[1]

To get the position of each sequence with positive entries:

pos = 0
for k, g in groupby(series, key=lambda i: i > 0):
    sub_list = list(g)
    L = len(sub_list)
    a = float(np.sum(sub_list))
    if a > 0: 
        xx.append(a)
        yy.append(L)
        sub_pos = tuple((pos, pos+L))
        print(tuple(sub_list), sub_pos)
    pos += L

For every sequence with positive entries, the variable sub_pos is generated (and printed). Just store them instead of/in addition to printing them.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Wierichs