1

I have a list such as [6,6,2,6,6,7,5,2,0,5,6].
I can enumerate it to get the list [(0,6),(1,6),(2,2),(3,6),(4,6),(5,7),(6,5),(7,2),(8,0),(9,5),(10,6)] very easily.
Then, what I want is to save the values of every index which has a 2nd value >= 5; that is, the output of that would be [0, 1, 3, 4, 5, 6, 9, 10].
I can do this very well so far. Now, what I desire is to parse this in this fashion:
[(0,1),(3,4,5,6),(9,10)]
Separating the items into groups where the difference between any two adjacent items is exactly 1.

My code so far:

    histo = [6,6,2,6,6,7,5,2,0,5,6]
    histo = list(enumerate(histo))
    abarrote = [x[0] for x in histo if x[1] >= 5]
    #Out[1]: [0, 1, 3, 4, 5, 6, 9, 10]
    #Desired output: [(0,1),(3,4,5,6),(9,10)]

Thanks in advance.

  • I don't think you can do this with tuples in python because they are immutable – bigwillydos Sep 07 '18 at 23:34
  • 1
    If you dn't want to install `more_itertools`, as the current accepted answer uses, use [this](https://stackoverflow.com/a/2154437/5014455) answer, which used to be the accepted one. It is quite elegant. – juanpa.arrivillaga Sep 07 '18 at 23:34
  • @bigwillydos sure you can. You can always create new tuples, indeed, tuples already understand the `+` concatenation operator. – juanpa.arrivillaga Sep 07 '18 at 23:35

0 Answers0