简体   繁体   English

示例中需要解释 lambda 行为

[英]Need explanation of lambda behavior in example

I am looking in to python code which is part of reinforcement learning我正在研究作为强化学习一部分的 python 代码

LEFT, RIGHT = range(2)
pi = lambda s: {
    0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT
}[s]

for s in range(7):
    print(pi(s))

output:
0
0
0
0
0
0
0

My question is what is [s] at lambda is present?我的问题是 lambda 的 [s] 是什么? what lambda behavior in this context. lambda 在这种情况下的行为。 Is dictionary kept in list?字典是否保存在列表中? If dictionary kept in list we have to access though list we have to put pi[0][s] in print right?如果字典保存在列表中,我们必须通过列表访问,我们必须将 pi[0][s] 放入打印中,对吗? Kindly explain请解释

pi = lambda s: {
    0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT
}[s]

is nothing more than无非就是

def pi(s):
    return {0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT}[s]

where在哪里

{0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT}[s]

is getting in a dict (here {0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT} ) the attribute for the key s .正在获取键s的属性(此处为{0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT} )。 So {0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT}[1] gives LEFT because LEFT is the attribute for key 1 .所以{0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT}[1]给出LEFT因为LEFT是键1的属性。

The above code defines this dictionary:上面的代码定义了这个字典:

dict = {
    0:LEFT, 1:LEFT, 2:LEFT, 3:LEFT, 4:LEFT, 5:LEFT, 6:LEFT
}

or alternatively ( left = 0 ):或者( left = 0 ):

dict = {
    0:0, 1:0, 2:0, 3:0, 4:0, 5:0, 6:0
}

The pi lambda returns dict[s] where s is the key in dictionary. pi lambda 返回dict[s]其中s是字典中的key So calling pi(s) is equal to returning dict[s] for key = s .所以调用pi(s)等于返回dict[s] for key = s

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM