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(' ', ',')