pythondictionarynested-loopspython-itertoolsword-list

How to generate all combinations of letters and digits but in a specific format


I'm trying to create a wordlist generator that creates a file with all possible combinations of uppercase letters and numbers but in this very specific format:

AAA00AA (uppercase, uppercase, uppercase, digit, digit, uppercase, uppercase)

So the first string would be AAA00AA and the last one ZZZ99ZZ. There are over 1 billion possible combinations and I'm using the itertools.product function.

However, I'm stuck on how to loop through the results of each iteration in order to get each group (AAA 00 AA) to combine among themselves. Here's what I got so far, but each loop runs just once. For example when the first group AAA 00 AA reaches ZZZ 00 AA I then need to get the second group through 1 iteration to AAA 01 AA and so on until the third group.

I'm sure that my loop nesting logic is wrong or perhaps I need to use some other approach, but I have no idea what to do. Can anyone help, please? Here's my code so far.

import string
import itertools
import datetime

letters = string.ascii_uppercase
digits = string.digits

first_group = itertools.product(letters, repeat=3)
second_group = itertools.product(digits, repeat=2)
third_group = itertools.product(letters, repeat=2)

FILE = open("mylist.txt","w")
start = datetime.datetime.now()

for i in first_group:
    first = ''.join(i)
    FILE.write(first + '\n')
    for a in second_group:
        second = first +''.join(a)
        FILE.write(second + '\n')
        for x in third_group:
            string = second +''.join(x)
            FILE.write(string + '\n')
            string = ''

FILE.close()
print 'DONE! - Finished in %s' % (datetime.datetime.now() - start)

Solution

  • You can use itertools.product to join the sub products again.

    f, s, t = [
        itertools.product(d, repeat=r) 
        for d, r in zip([letters, digits, letters], [3, 2, 2])
    ]
    with open("mylist.txt", "w") as f:
        for prod in itertools.product(f, s, t):
            string = ''.join([''.join(k) for k in prod])
            f.write(string + '\n')
    
    # AAA00AA
    # AAA00AB
    # AAA00AC
    # AAA00BA
    # AAA00BB
    # .......