1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | def list_to_pattern(self, lst):
ret = ''
lst.sort()
start = None
# Starts from second item
for i in range(1, len(lst)):
# In sequence
if lst[i] == lst[i-1] + 1:
if start is None: start = lst[i-1]
# Last item
if i == len(lst) - 1:
ret += ' %d-%d' %(start, lst[i])
# Sequence broked
else:
# Last item
if start is None:
ret += ' %d' % lst[i-1]
else:
ret += ' %d-%d' %(start, lst[i])
return ret.strip().replace(' ', ',')
|
More like this
- Pattern to integer list function by marinho 5 years, 6 months ago
- hide_email by rukeba 5 years, 1 month ago
- SuperChoices by willhardy 4 years, 7 months ago
- Scoped Cache Compatible with Django Caching Helpers by axiak 5 years, 3 months ago
- Model Choices Helper by pmclanahan 3 years, 4 months ago
Comments