1

everyone.

My case is the following one: I have tried to use lambda functions as dictionary key values. The point is that I want to do an operation that involves the specefic key of its value.

def constraints(var):
    pars=[(x,y) for x in var for y in var if x!=y and var.index(x) < var.index(y)]
    cons=dict()
    for i in range(0, len(pars)):
            rest= pars[i][0]-pars[i][1]
            cons[pars[i]]= lambda x, y: abs(x-y)!=abs(rest)
    return cons

I have debugged because with this example print(constraints([1,2,3,4])[(1,4)](2,3)) it returns me false instead of true and I found out that when print function calls constraints function, the value of "i" is the one of the last iteration and rest is -1 when it had to be -3.

Thank you.

Satinfive
  • 11
  • 1

0 Answers0