Skip to content

Instantly share code, notes, and snippets.

@osvalr
Last active September 22, 2017 15:34
Show Gist options
  • Save osvalr/7483b46eebd2b9dc292a082cd3e68a48 to your computer and use it in GitHub Desktop.
Save osvalr/7483b46eebd2b9dc292a082cd3e68a48 to your computer and use it in GitHub Desktop.
recognize gaps in a list of sequences of numbers, and group them
#!/usr/sbin/python
# Taken from Python examples: https://docs.python.org/2.6/library/itertools.html#examples
from operator import itemgetter
from itertools import groupby
data_list = [[1,3], [1, 2, 3], [3]]
for data in data_list:
for k, g in groupby(enumerate(data), lambda ix :ix[0]-ix[1]):
print map(itemgetter(1), g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment