本文介绍了如何遍历itertools.product()的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现Q学习算法,我的状态空间在给定长度的向量中包含数字0,1,2的所有可能组合.

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.

现在,我正在尝试初始化一个全为零的Q表,该表的行数与我的状态空间相同.然后,我想在每一步中遍历状态空间并检查所有可能的状态向量中的哪一个现在正确.但这意味着我必须下标itertools.product()我怎样才能做到这一点?因为当我尝试从乘积中打印第n个矢量时,它显示出乘积不可下标的错误

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

我尝试过:

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]))

还有可能,如何删除 length 变量?因为当我将for循环定义为: for范围(len(list(state(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.

非常感谢您

推荐答案

您只能对 product 的一个实例进行一次迭代:在此之后,该实例将被使用. list 遍历该实例,以生成您要计算其长度的列表.一旦这样做,状态空间就消失了.剩下的就是长度.

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)

这篇关于如何遍历itertools.product()的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:32