Reputation: 1550
I'm using the python itertools module to create a generator that iterates from aaa
to ccc
.
I cant figure out a way to start the iteration from a certain position
for example if the input is aba
the iteration will continue from that position
this is how my code looks like now:
from itertools import product
strings = itertools.product(*["abc"]*3)
for item in strings:
print("".join(item))
Upvotes: 0
Views: 382
Reputation: 61014
This method doesn't skip any of the computation, it just drops values until it sees the one it's looking for. This should work on any iterable, but there may be a product
specific solution that would allow you to skip generating the values you don't care about.
from itertools import dropwhile, product
def resume(iterable, sentinel):
yield from dropwhile(lambda x: x != sentinel, iterable)
for t in resume(product('abc', repeat=3), ('a', 'b', 'a')):
print(*t)
Upvotes: 3