def ranges iterable iterable iter sorted set iterable start stop itera

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
def ranges(iterable):
iterable = iter(sorted(set(iterable)))
start = stop = iterable.next()
for cur in iterable:
stop += 1
if cur != stop:
yield yield_range(start, stop, '-')
start = stop = cur
yield yield_range(start, stop, '-')
def yield_range(start, stop, delimiter):
if start == stop:
return str(stop)
else:
return "%d%s%d" % (start, delimiter, stop - 1)