:
list='abcdefg'
depth=8
def generate(l,d):
if d<1:
return
for c in l:
if d==1:
yield c
else:
for k in generate(l,d-1):
yield c+k
for d in range(1,depth):
for c in generate(list,d):
print c
, , itertools :
import itertools
chrs='abc'
n=6
for i in range(1,n):
for xs in itertools.product(chrs, repeat=i):
print ''.join(xs)
So you have all the words from length 1 to n in your list.
source
share