0

I have a DataFrame from which I'm filtering the values. Lets say that my data is x=[1 1 1 2 2 3 3 3 1 1 1 3 3 1 1]; and I'd like to have all the indexes where 3 is located. So I'm executing data=df[df.x==3] to get the list data.index=[5,6,7,11,12]

Now I'd like to generate a narrow list which will provides me the ranges where 3 is located: [[5, 7] [11, 12]]. Is there a simple way to do it rather than write a function for this?

In Matlab I'm doing this by following (idx - the list of indexes where 3 is located and a is an output)

a(1,1)=idx(1);
j=1;
for i=2:length(idx)
    if (idx(i) ~= (idx(i-1)+1))
        a(j,2)=idx(i-1);
        if i < length(idx)
            j=j+1;
            a(j,1)=idx(i);
        end
    end
end
a(j,2)=idx(end);
user1977050
  • 496
  • 1
  • 6
  • 18
  • I guess you are looking for "find all contiguous sub-sequences in a sequence"; has answers [here](https://stackoverflow.com/questions/24543672/google-interview-find-all-contiguous-subsequence-in-a-given-array-of-integers) – anurag Dec 31 '20 at 08:20
  • I don't think you will find a ready made module for `Is there a simple way to do it rather than write a function for this` in python! – anurag Dec 31 '20 at 08:21
  • "find all contiguous sub-sequences in a sequence" - this is the idea. but the link is to little bit other problem – user1977050 Dec 31 '20 at 08:25

1 Answers1

3

Are you using Python 3? I'm not sure what you mean by simple.

If you've got a list of data -> data = [5,6,7,11,12]

You can group them this way (Refer to Python documentation HERE)

from itertools import groupby
from operator import itemgetter

ranges =[]
data = [5,6,7,11,12]
for k, g in groupby(enumerate(data), lambda x:x[0]-x[1]):
    group = list(map(itemgetter(1), g))
    ranges.append([group[0], group[-1]])
print(ranges)

Gives you,

[[5, 7], [11, 12]]
Sin Han Jinn
  • 574
  • 3
  • 18