Home > OS >  Need explanation of lambda behavior in example
Need explanation of lambda behavior in example

Time:12-18

I am looking in to python code which is part of reinforcement learning

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? what lambda behavior in this context. 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? Kindly explain

CodePudding user response:

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. 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.

CodePudding user response:

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

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. So calling pi(s) is equal to returning dict[s] for key = s.

  • Related