Skip to content

Instantly share code, notes, and snippets.

@nemith
Created September 26, 2017 05:47
Show Gist options
  • Save nemith/52d1c25bddf3c372538773f0506d0169 to your computer and use it in GitHub Desktop.
Save nemith/52d1c25bddf3c372538773f0506d0169 to your computer and use it in GitHub Desktop.
import itertools
import operator
def range_str(l):
groups=[]
for _, g in itertools.groupby(enumerate(l), lambda (i,x):i-x):
group = map(operator.itemgetter(1), g)
if len(group) > 1:
groups.append("{}-{}".format(group[0], group[-1]))
else:
groups.append(str(group[0]))
return ",".join(groups)
print(range_str([11,12,15,16,17,18]))
@jpsenior
Copy link

This is great! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment