Created
September 26, 2017 05:47
-
-
Save nemith/52d1c25bddf3c372538773f0506d0169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is great! Thanks!