0

I am implementing Minimum Remaining Values of CSP in python.And I got some errors.

I run with python3 and also with python2 interpreter .

def select_unassigned_variable(assignments, csp):
    variables = [var for var in csp.nodes()
                     if var not in assignments.keys()]
    if not variables:
        return None
    return min(variables, key=(lambda var: (len(csp.nodes[var]['domain']))))

I got the error like:

return min(variables, key=(lambda var: (len(csp.nodes[var]['domain']))))
TypeError: 'method' object is not subscriptable
Russ Hyde
  • 2,154
  • 12
  • 21
Cecelia
  • 1
  • 6

1 Answers1

1

change to something like

key=(lambda var: (len(csp.nodes()[var]['domain']))))
SuperStew
  • 2,857
  • 2
  • 15
  • 27
  • Still error.list indices must be integers or slices, not str – Cecelia Mar 27 '19 at 16:26
  • Thats because `nodes` is (apparently) a list, and you have not defined `var` as an integer. It seems the only reason it has a value at all is because it hasn't been trash collected yet has left over value from the list comprehension. – SuperStew Mar 27 '19 at 16:35