Andrew Summers
Andrew Summers

Reputation: 3

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)

Upvotes: 0

Views: 2502

Answers (4)

muhammad k
muhammad k

Reputation: 1

i am also facing kinda similar issue. I'm new to python too, i am generating a password combination of 6 letters like "AAAAAA" ends with "ZZZZZZ" with a few conditions, then puts it in a .txt file, when i run the code it takes a while then errors out MEMORYERROR. How can i make the code more efficient and less memory consuming. thanks here is the code

from itertools import permutations from string import ascii_lowercase

def generate_password_combinations():

# Define the valid letters for the password
valid_letters = 'abcdefghijklmnopgrstuvwxyz'

# Generate all permutations of the valid letters
permutations_list = permutations(valid_letters, 6)

# Filter out permutations that violate the repetition rule
filtered_permutations = []
for permutation in permutations_list:
    valid = True
    for i in range(len(permutation) - 2):
        if permutation[i] == permutation[i+1] and permutation[i] == permutation[i+2]:
            valid = False
            break
    if valid:
        filtered_permutations.append(permutation)

# Convert the permutations to uppercase strings
password_combinations = [''.join(perm).upper() for perm in filtered_permutations]

# Write the password combinations to a text document
with open('passwords_Combo1.txt', 'w') as file:
    for combination in password_combinations:
        file.write(combination + '\n')

return password_combinations

combinations = generate_password_combinations() print(f"Generated {len(combinations)} password combinations.") print("The combinations have been written to 'passwords_Combo1.txt'.")`

Upvotes: 0

cs95
cs95

Reputation: 402962

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
# .......

Upvotes: 3

Netwave
Netwave

Reputation: 42766

Use a list comprehension:

res = ["".join(itertools.chain(a,b,c)) for c in third_group for b in second_group for a in first_group] 
res
['AAA00AA', 'AAB00AA', 'AAC00AA', 'AAD00AA', 'AAE00AA', 'AAF00AA', 'AAG00AA', 'AAH00AA', 'AAI00AA', 'AAJ00AA', 'AAK00AA', 'AAL00AA', 'AAM00AA', 'AAN00AA', 'AAO00AA', 'AAP00AA', 'AAQ00AA', 'AAR00AA', 'AAS00AA', 'AAT00AA', 'AAU00AA', 'AAV00AA', 'AAW00AA', 'AAX00AA', 'AAY00AA',...]

You can even make it a generator object:

for e in ("".join(itertools.chain(a,b,c)) for c in third_group for b in second_group for a in first_group):
    print e

Upvotes: 0

Ma0
Ma0

Reputation: 15204

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)

start = datetime.datetime.now()

with open("mylist.txt","w") as FILE:
    for i in first_group:
        first = ''.join(i)
        for j in second_group:
            second = ''.join(j)
            for k in third_group:
                FILE.write(first + second + ''.join(k) + '\n')

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

Generates:

AAA00AA

AAA00AB

AAA00AC

AAA00AD

AAA00AE

AAA00AF

...

Everything else you can leave as they are. the itertools.product solution by @Coldspeed is however more elegant and probably faster too. I just wanted to correct your code.

Upvotes: 0

Related Questions