0

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?

asked Jul 16, 2010 at 5:45

2 Answers 2

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?!

answered Jul 16, 2010 at 5:55
Sign up to request clarification or add additional context in comments.

11 Comments

I am going to use the (4,5) as the key and other 2 as values. so I shud do "s = problem.getSuccessors(getStartState())" closed[s[0]] = s[1:] If I need to use only the key, then wat shud I do? Because later on, closed to going to have a lot of entries like ((4,5),north,1).
And ya if forget to mention,,it is 'north'. NOT north string
If you "need to use only the key" (and never the value), then, use a 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...).
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?
I always appreciate your response.You are the best.
|
1
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.

answered Jul 16, 2010 at 5:48

1 Comment

"your_key" can be any variable - like index What if I need to take out the first thing (4,5) from it, what are the commands I shud use then?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.