Skip to content Skip to sidebar Skip to footer

How Can I Iterate Through The Result Of Itertools.product()?

I am trying to implement a Q-Learning algorithm, my state-space contains all possible combinations of numbers 0,1,2 in a vector of a given length. Now I am trying to initialize a Q

Solution 1:

You can only iterate over an instance of product once: after that, it is consumed. list iterates over the instance in order to produce a list whose length you compute. Once you do that, the state space is gone; all you have left is the length.

You don't need to convert the state space to a list or compute its length; you can just iterate over it directly:

state_space = itertools.product([0,1,2], repeat=NUMBER_OF_SECTORS)
forstate in state_space:
    print(state)

Post a Comment for "How Can I Iterate Through The Result Of Itertools.product()?"