2

I am trying to generate custom alpha numeric sequence. The sequence would be like this :

AA0...AA9 AB0...AB9 AC0...AC9..and so on..

In short, there are 3 places to fill..

On the first place, the values can go from A to Z.
  On the second place, the values can go from A to Z.
    On the last place, the value can go from 0 to 9.

Code :

  s= list('AA0')
  for i in range(26):
    for j in range(26):
        for k in range(10):
            if k<10:
                print(s[0]+s[1]+str(k))
        s[1]= chr(ord(s[1])+1)
    s[0]= chr(ord(s[0])+1)  

I was able to generate sequence till AZ9 and then I am getting below sequence.. it should be BA0...BZ9..

B[0
B[1
B[2
B[3
B[4
B[5
B[6
B[7
B[8
B[9
B\0
B\1
B\2
B\3
B\4
B\5
B\6

2 Answers 2

1

this is a way to do just that:

from itertools import product
from string import ascii_uppercase, digits

for a, b, d in product(ascii_uppercase, ascii_uppercase, digits):
    print(f'{a}{b}{d}')

string.ascii_uppercase is just 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; string.digits is '0123456789' and itertools.product then iterates over all combinations.

instead of digits you could use range(10) just as well.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use itertools.product:

>>> letters = [chr(x) for x in range(ord('A'), ord('Z')+1)]
>>> letters
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
>>> combinations = ["".join(map(str, x)) for x in itertools.product(letters, letters, range(10))]
>>> combinations
['AA0', 'AA1', 'AA2', 'AA3', 'AA4', 'AA5', 'AA6', 'AA7', 'AA8', 'AA9', 'AB0', 'AB1', 'AB2', 'AB3', 'AB4', 'AB5', 'AB6', 'AB7', 'AB8', 'AB9', 'AC0', 'AC1', 'AC2', 'AC3', 'AC4', 'AC5', 'AC6', 'AC7', 'AC8', 'AC9', 'AD0', 'AD1', 'AD2', 'AD3', 'AD4', 'AD5', 'AD6', 'AD7', 'AD8', 'AD9', 'AE0', 'AE1', 'AE2', 'AE3', 'AE4', 'AE5', 'AE6', 'AE7', 'AE8', 'AE9', 'AF0', 'AF1', 'AF2', 'AF3', 'AF4', 'AF5', 'AF6', 'AF7', 'AF8', 'AF9', 'AG0', 'AG1', 'AG2', 'AG3', 'AG4', 'AG5', 'AG6', 'AG7', 'AG8', 'AG9', 'AH0', 'AH1', 'AH2', 'AH3', 'AH4', 'AH5', 'AH6', 'AH7', 'AH8', 'AH9', 'AI0', 'AI1', 'AI2', 'AI3', 'AI4', 'AI5', 'AI6', 'AI7', 'AI8', 'AI9', 'AJ0', 'AJ1', 'AJ2', 'AJ3', 'AJ4', 'AJ5', 'AJ6', 'AJ7', 'AJ8', 'AJ9', 'AK0'...]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.