[Tutor] All possible 16 character alphanumeric strings?
eryksun
eryksun at gmail.com
Sun Sep 16 18:50:24 CEST 2012
On Sat, Sep 15, 2012 at 6:50 PM, Scurvy Scott <etanes.rm at gmail.com> wrote:
>
> I'm trying to generate a list of every possible 16 character string
> containing only 2-7 and a-z lowercase.
This type of problem is just nested iteration:
>>> seq = '01'
>>> r = []
>>> for g0 in seq:
... for g1 in seq:
... for g2 in seq:
... r.append( ''.join([g0, g1, g2]))
...
>>> r
['000', '001', '010', '011', '100', '101', '110', '111']
You can generalize this with a recursive generator, or use
itertools.product(seq, repeat=N):
def seq_prod(seq, n):
if n > 1:
for left in seq:
for right in seq_prod(seq, n - 1):
yield (left,) + right
elif n == 1:
for item in seq:
yield (item,)
>>> [''.join(s) for s in seq_prod('01', 3)]
['000', '001', '010', '011', '100', '101', '110', '111']
>>> [''.join(s) for s in itertools.product('01', repeat=3)]
['000', '001', '010', '011', '100', '101', '110', '111']
More information about the Tutor
mailing list