wordlist generator with specific algorithms using python

388 Views Asked by At

Please, I need to create a wordlist like this one "X4K7GB9y". of 8 in length

(Letter Uppercase) (Number) (Letter Uppercase) (Number) (Letter Uppercase) (Letter Uppercase) (Number) (letter Lowercase)

with all the possibilities without repetition using python I´ll appreciate it if you give me a hint Thank you in advance

2

There are 2 best solutions below

2
DanielTuzes On

You can use random.sample and select k=8 pieces from the list you want.

To meet your requirements, you can generate the characters in the individual categories (uppercase, lowercase, digits) without repetition, and reorder them like. You can put it into a loop, and write the result into a file.

import random
import string

random.seed(0)
NUM_WORDS = 10
with open("wordlist.txt","w",encoding="utf-8") as ofile:
    for _ in range(NUM_WORDS):
        uppc = random.sample(string.ascii_uppercase,k=4)
        lowc = random.sample(string.ascii_lowercase,k=1)
        digi = random.sample(string.digits,k=3)
        word = uppc[0] + digi[0] + uppc[1] + digi[1] + uppc[2] + uppc[3] + digi[2] + lowc[0]
        print(word,file=ofile)

Is this what you want? Or did you mean something else by without repetition?

0
Nicolai B. Thomsen On

So, in theory, the simple way of doing it is to get all permutations like so.

from itertools import product
from string import ascii_uppercase, digits

ups = ascii_uppercase
lows = ascii_lowercase

for x in product(ups, digits, ups, digits, ups, ups, digits, lows):
    print("".join(x))

However, in practice you will most likely run out of memory. Please be aware that there is a LOT of permutations (11881376000 to be exact), so you will most likely want to get a subset of them. You could that this way, where n is the the number of permutations you want.

def alphastring_generator(pattern, n):

    for idx, x in enumerate(product(*pattern)):
        if idx > n:
             break
        yield "".join(x)

my_pattern = [ups, digits, ups, digits, ups, ups, digits, lows]

result = [*alphastring_generator(my_pattern, n=1000)]