Integer list to pattern function

 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

  1. Pattern to integer list function by marinho 6 years, 4 months ago
  2. Model Choices Helper by pmclanahan 4 years, 2 months ago
  3. Filter by first letter inclusion tag by slafs 3 years, 11 months ago
  4. SuperChoices by willhardy 5 years, 5 months ago
  5. Yet another SQL debugging facility by miracle2k 6 years, 8 months ago

Comments

(Forgotten your password?)