파란하늘
파란하늘

Reputation: 11

Generating every possible string combination in python

I am new to python and has been trying to make this algorithm but couldn't figure it out. How could I make a list of every possible word made of a-Z, so that it goes like

a, b, c, d, e.... aa, ab, ac, ad... aaa, aab, aac...The purpose of doing this, is to encode every word into md5 hash and repeat this until finding a hash code that has a specific prefix.

So, how could I achieve this? Thank you in advance for your help!

Upvotes: 1

Views: 1031

Answers (3)

Kelly Bundy
Kelly Bundy

Reputation: 27588

How about a recursive generator?

from string import ascii_lowercase
from itertools import islice

def words():
    yield ''
    for word in words():
        for char in ascii_lowercase:
            yield word + char

print(' '.join(islice(words(), 30)))
print(' '.join(islice(words(), 0, 10**8, 10**7)))

Output:

 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 aa ab ac
 uvxwj aqswtt bmpvrd cimuon dejtlx eagsjh ewdrgr fsaqeb gnxpbl

Upvotes: 0

kederrac
kederrac

Reputation: 17322

you can use a generator expression and itertools.product:

from string import ascii_lowercase
from itertools import product, count

gen = (''.join(i) for r in count(1) for i in product(ascii_lowercase, repeat=r))

every time you want a combination you can use the built-in function next:

next(gen)

generators are memory friendly and if you want to generate a huge amount of strings that will be process somewhere else (in other function for ex) it will be more convenient to not keep all the combinations in memory

Upvotes: 2

yatu
yatu

Reputation: 88226

You can use itertools.product (since you want the cartesian product of all letters up to a given length) and a list comprehension to iterate over a range up to the desired length (4 in your example):

from string import ascii_lowercase
from itertools import product

n = 4
[''.join(i) for r in range(1,n) for i in product(ascii_lowercase, repeat=r)]

Which gives:

['a',
 'b',
 'c',
 'd',
 'e',
 'f',
 'g', 
 ...   
 'aa',
 'ab',
 'ac',
 'ad',
 'ae',
 'af',
 'ag',
 'ah',
 'ai',
 'aj',
  ...
 'zz',
 'aaa',
 'aab',
 'aac',
 'aad',
 'aae',
 'aaf',
 'aag',
  ...

Upvotes: 4

Related Questions