problem.getSuccessors(getStartState()) - it returns something like ( (4,5) , north, 1) - that means 3 things - tuple, direction,cost.
I'm using a dictionary ,closed = {}
Now I need to put the output of above function in the dictionary "closed" - how can I do that??
I need to use only dictionary because I need to return "action" i.e North, south....at the end of function. after doing some iterations, my dict is going to have multiple entries such as ((4,5),north,1) , ((3,4),south,1) and I need to extract the key from the dict i.e (4,5) .what shud I use?
2 Answers 2
I need to put the output of above function in the dictionary "closed" - how can I do that??
It entirely depends on what you want to use as the key, and what as the value! If the key is something completely unrelated to the tuple ( (4,5) , north, 1) (I'm not sure what the north identifier is supposed to be or how it got thee -- you sure it's not the string 'north' instead?!), then @mipadi's answer is correct. If the first item (the nested tuple) is the key, the other two the value, then, after
s = problem.getSuccessors(getStartState())
you'll do:
closed[s[0]] = s[1:]
If the "tuple and direction", together, are the key, and just the cost is the value, then you'll do, instead:
closed[s[:2]] = s[2]
So, what is it that you intend to use as the key into the dictionary closed?!
11 Comments
set, not a dict (just make it with closedset=set() and add entries with closedset.add(s[0])!). If instead what you mean is that there are many direction-cost entries for each given value of the "tuple" (like (4, 5)), then import collections, use closed = collections.defaultdict(list), and closed[s[0]].append(s[1:]). If you could explain your needs with any clarity at all, helping you would be much, much easier (and I note you still haven't accepted my other answer you praised...).closed = {}
# ...
closed["your_key"] = problem.getSuccessors(getStartState())
Responding to comment:
If by "take out" you mean you want (4,5) to be the key and (north, 1) to be the value, you can slice the tuple:
val = problem.getSuccessors(getStartState())
closed[val[0]] = val[1:]
If you just want to drop the (4,5), you can also slice the tuple:
closed["your_key"] = problem.getSuccessors(getStartState())[1:]
And yes, you can use a variable as a key instead of a hard-coded string.