You can do it like that:
(You'll find an explanation after that code)
from itertools import groupby
from operator import itemgetter
# PART 1
# List that will contain lists of indexes of "chip_nums".
# Each list of indexes will correspond to consecutive chips values
list_of_indexes =list()
for k,g in groupby(enumerate(chip_nums),lambda x:x[0]-x[1]):
group = (map(itemgetter(0),g))
group = list(map(int,group))
list_of_indexes.append(group)
# PART 2
# From the retrieved indexes, get fist value in tuple ("index in tuple")
list_of_indexes_contained_in_tuples = list()
for indexes in list_of_indexes:
consecutive_indexes = list()
for index in indexes:
tuple_in_processed = processed[index]
index_in_tuple = tuple_in_processed[0]
consecutive_indexes.append(index_in_tuple)
list_of_indexes_contained_in_tuples.append(consecutive_indexes)
print(list_of_indexes_contained_in_tuples)
Output:
[[1, 1, 1, 2], [4, 4, 4, 4, 5], [7, 7, 7, 7, 8], [10, 10, 10], [12, 12], [14], [16, 16], [18, 18], [20, 20, 20, 20], [23], [25], [27, 27]]
Explanation:
The "PART 1" is a modified version of the function consecutive_groups from more_itertools.
I don't know if you knew that, the source code of consecutive_groups (https://github.com/more-itertools/more-itertools/blob/master/more_itertools/more.py) is an implementation of this recipe that one can find on Python.org here: https://docs.python.org/2.6/library/itertools.html#examples
This recipe was first written in Python 2.6:
from operator import itemgetter
from itertools import groupby
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda (i,x):i-x):
print map(itemgetter(1), g)
Transposed in Python 3, you will have this:
from operator import itemgetter
from itertools import groupby
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda x:x[0]-x[1]):
print(list(map(itemgetter(1), g)))
The modification consists of an adaptation of the lambda function to Python 3.
Note that we also wrapped the map function with list() in order to obtain list objects insead of map objects.
As for now, with this function we get what you get with the code you provided.
Output:
[2, 3, 4, 5]
[12, 13, 14, 15, 16, 17]
Now, we will modify this function in order to get the indexes of the elements instead of the values in the list (here named "data"). To that end, we just have to pass 0 as argument to itemgetter instead of 1:
data = [2, 3, 4, 5, 12, 13, 14, 15, 16, 17]
for k, g in groupby(enumerate(data), lambda x:x[0]-x[1]):
print(list(map(itemgetter(0), g)))
No, we have the indexes.
Output:
[0, 1, 2, 3]
[4, 5, 6, 7, 8, 9]
The second part just uses the indexes to get the first items inside the tuples (so the indexes you want).
So, in a nutshell, I adapted the original function consecutive_groups from more_itertools in order to return the indexes of the items instead of their values, and used these indexes to retrieved the first values in each tuple.