Increment Alphabet Python
Question: I have a list like this

    
    
    ['AX95', 'BD95']
    

I need to expand the list starting from `AX95` to `BD95` like this

    
    
    ['AX95', 'AY95', 'AZ95', 'BA95','BB95','BC95','BD95']
    

My current code works fine for single alphabets like

    
    
    ['A95', 'D95']
    

My code looks like this for now,

    
    
    import re
    
    
    def resolve(item):
        start = int(re.search(r'\d+', item[0]).group())
        end = int(re.search(r'\d+', item[1]).group())
        print(item)
        c = re.search(r'[a-zA-Z]+', item[0]).group()
        d = re.search(r'[a-zA-Z]+', item[1]).group()
        print(c, d)
        for char in range(ord(c), ord(d) + 1):
            yield chr(char) + str(end)
    
    
    xx = resolve(['AX95', 'BD95'])
    
    print(list(xx))
    

How to do this?


Answer: You cannot directly use `ord()` on multiple characters , it would error out
with the error -

    
    
    TypeError: ord() expected a character, but string of length 2 found
    

Also , it would be very complicated to do this with `for` loop and `range()` ,
I would suggest using `while` loop and checking till the start characters
become the end characters.

One way to do this would be to get the last element, check if its `Z` change
it to `A` and increment the element before it. Otherwise take its `ord()`
increment by `1` and then get that character using `chr()` .

Example Algorithm that works on arbitrary size of characters -

    
    
    def resolve(item):
        start = int(re.search(r'\d+', item[0]).group())
        c = re.search(r'[a-zA-Z]+', item[0]).group()
        d = re.search(r'[a-zA-Z]+', item[1]).group()
        print(c, d)
        s = c
        yield s + str(start)
        while s != d:
            ls = len(s) - 1
            news = ""
            for i in range(ls,-1,-1):
                c = s[i]
                if c.upper() == 'Z':
                    news += 'A'
                else:
                    news += chr(ord(c) + 1)
                    break
            s = s[:i] + news[::-1]
            yield s + str(start)
    

Example/Demo -

    
    
    >>> def resolve(item):
    ...     start = int(re.search(r'\d+', item[0]).group())
    ...     c = re.search(r'[a-zA-Z]+', item[0]).group()
    ...     d = re.search(r'[a-zA-Z]+', item[1]).group()
    ...     print(c, d)
    ...     s = c
    ...     yield s + str(start)
    ...     while s != d:
    ...         ls = len(s) - 1
    ...         news = ""
    ...         for i in range(ls,-1,-1):
    ...             c = s[i]
    ...             if c.upper() == 'Z':
    ...                 news += 'A'
    ...             else:
    ...                 news += chr(ord(c) + 1)
    ...                 break
    ...         s = s[:i] + news[::-1]
    ...         yield s + str(start)
    ...
    >>>
    >>> xx = resolve(['AX95', 'BD95'])
    >>>
    >>> print(list(xx))
    AX BD
    ['AX95', 'AY95', 'AZ95', 'BA95', 'BB95', 'BC95', 'BD95']
    

