Good morning, I saw this post this and the question asked was how can I make a script/function that makes every possible combination of letters. I found the code from user Kevin pretty useful so I started using it. although sometimes my code stops and it has to go all over again.
My question is: how can I start this code (see below) with an string that I choose. for example if I type somewhere bbbb it's going to generate every possible combination between bbbb and cccc.
from string import ascii_lowercase
import itertools
def iter_all_strings():
for size in itertools.count(1):
for s in itertools.product(ascii_lowercase, repeat=size):
yield "".join(s)
for s in iter_all_strings():
print(s)
if s == 'cccc':
break
EDIT: so I found the source code of itertools.product() and it is this:
def product(*args, repeat=1):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = [tuple(pool) for pool in args] * repeat
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
source: https://docs.python.org/3/library/itertools.html#itertools.combinations Does anyone how can I accomplish my goal with this code?