一个在python句子里面的for循环

新手学python,我遇到一个python在句子里面,我不能理解这个for循环是什么意思。
如果改成常见的那种for循环应该怎么写。
我看不懂的代码是这一句

 Q[s, a] = np.sum([T[s, a, sp] * (R[s, a, sp] + discount_rate * np.max(Q_prev[sp]))    for sp in range(3)])

这是完整的代码,运行的环境是jupyter,python版本是3.6.5

 import numpy as np
nan = np.nan
T = np.array([[[0.7,0.3,0.0],[1.0,0.0,0.0],[0.8,0.2,0.0]],
              [[0.0,1.0,0.0],[nan,nan,nan],[0.0,0.0,1.0]],
              [[nan,nan,nan],[0.8,0.1,0.1],[nan,nan,nan]]
             ])
R = np.array([[[10.,0.0,0.0],[0.0,0.0,0.0],[0.0,0.0,0.0]],
              [[10.,0.0,0.0],[nan,nan,nan],[0.0,0.0,-50.0]],
              [[nan,nan,nan],[40.0,0.0,0.0],[nan,nan,nan]]
             ])
possible_actions = [[0,1,2],[0,2],[1]]

Q = np.full((3, 3), -np.inf)  # -inf 对应着不可能的动作 
for state, actions in enumerate(possible_actions):    
    Q[state, actions] = 0.0  # 对所有可能的动作初始化为0.0
learning_rate = 0.01 
discount_rate = 0.95 
n_iterations = 100
for iteration in range(n_iterations):   
    Q_prev = Q.copy()    
    for s in range(3):        
        for a in possible_actions[s]:            
            Q[s, a] = np.sum([T[s, a, sp] * (R[s, a, sp] + discount_rate * np.max(Q_prev[sp]))  
            for sp in range(3)])

1个回答

这句话改成常见写法是:
for sp in range(3):
Q[s, a] += T[s, a, sp] * (R[s, a, sp] + discount_rate * np.max(Q_prev[sp]))

举个例子:
t = [i for i in range(3)]
相当于:
t = []
for i in range(3):
t.append(i)
这是一种非常方便的写法。

cjk_cjk
cjk_cjk 回复qq_34644971: 你好,这是因为在我们加的for循环前面需要初始化,Q[s,a]=0.0
接近 2 年之前 回复
qq_34644971
qq_34644971 你好,我把有疑问的那一句换成了你说的那种写法,然后输出Q,结果和原本的代码输出Q,结果是不一样的。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问