Skip to content Skip to sidebar Skip to footer

How To Get New Input To Generator In Python Without Create A New Generator

I try to write code that gets a list and generates all of this transformations by using yield statement. The problem is when I want to get new input to generator by using send func

Solution 1:

Why not just create a new object of type permute and use it

import itertools
def permute(items):
    permutations = [x for x in itertools.permutations(items)]
    permutations.sort()
    for n in permutations:
        yield (n)

g = permute(['b','a','c'])
print(next(g)) #('a', 'b', 'c')
print(next(g)) #('a', 'c', 'b')

g =  permute(['e','q','c'])
print(next(g)) #('b', 'c', 'a') need to be ('c', 'e', 'q')
#I get ('c', 'e', 'q')

Post a Comment for "How To Get New Input To Generator In Python Without Create A New Generator"