Search code examples
pythonfor-looppython-itertools

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-Table full of zeros which would have the same amount of rows as my state-space. And then I want to in each step to run through the state space and check which of all possible state vector is right now. But that means I have to subscript an itertools.product() How can I do that? because when I try to print it n-th vector from the product it shows an error that product is not subscriptable

I tried this:

import itertools
NUMBER_OF_SECTORS = 6
state_space = itertools.product(*[[0, 1, 2]] * NUMBER_OF_SECTORS)
length = len(list(state_space)) # 729
       for obs in range(length):
            print(list(state_space[obs]))

Also, is there a possibility, how can I rid the length variable? Because when I define the for loop as: for obs in range(len(list(state_space))) it is not executed at all.

Thank you very much


Solution

  • 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)
    for state in state_space:
        print(state)