[Tutor] All possible 16 character alphanumeric strings?

Peter Otten __peter__ at web.de
Sun Sep 16 01:30:01 CEST 2012


Scurvy Scott wrote:

> Hello again python tutor list.
> I have what I see as a somewhat complicated problem which I have no idea
> where to begin. I'm hoping you fine folks can help me.
> 
> I'm trying to generate a list of every possible 16 character string
> containing only 2-7 and a-z lowercase. I've seen some examples using regex
> to define which characters I want to use but not a way to generate the
> complete list of all possibilities. I'm not looking for a handout- just a
> point in the right direction.
> 
> Any information would be awesome, thanks.
> 
> Right now I've got something like:
> 
> import random
>>>> ''.join(random.choice('234567abcdefghijklmnopqrstuvwxyz') for i in
>>>> range(16))
> 
> Which only prints 1 number, obviously.
> 
> or possibly something like this:
> 
> 
> def genKey():
>     hash = hashlib.md5(RANDOM_NUMBER).digest().encode("base32")
>     alnum_hash = re.sub(r'[^a-z2-7]', "", hash)
>     return alnum_hash[:16]
> 
> 
> Keeping in mind that although I understand this code, I did not write it,
> I got it from stackoverflow.
> 
> Again any help would be great. Feel free to ask if you must know exactly
> what I'm trying to do.
> 
> I'm running Ubuntu 12.04 and python 2.7
> 
> Scott

from itertools import product
from string import ascii_lowercase

chars = ascii_lowercase + "234567"
assert len(chars) == 32

for item in product(*[chars]*16):
    print "".join(item)

Now, assuming this script will print 10 million 16-character strings per 
second it should terminate in...

>>> 32**16 / (10000000*60*60*24*365)
3833478626L

about four billion years.



More information about the Tutor mailing list