tags:

views:

75

answers:

2

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?

+1  A: 
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.

mipadi
"your_key" can be any variable - like indexWhat if I need to take out the first thing (4,5) from it, what are the commands I shud use then?
Shilpa
Yes, I'll address both those points in my answer.
mipadi
+2  A: 

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

Alex Martelli
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).
Shilpa
And ya if forget to mention,,it is 'north'. NOT north string
Shilpa
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...).
Alex Martelli
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?
Shilpa
I always appreciate your response.You are the best.
Shilpa
@Shilpa - click the green tick for the most useful answer. You should go and do it for other questions too.
detly
is that's the one alex is talking abt? i clicked on the tick.
Shilpa
@Shilpa - yep, that's the one. It basically signifies that "this answered my question," which gives the answerer reputation points. It's just the system here. You should go back over your other questions and do the same (unless there really was no good answer).
detly
ok...can u plz answer my question in the comments?I think, ALEX is busy somewhere.
Shilpa
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?
Shilpa
To loop on all a dict's keys, `for k in closed:`. To get all keys as a list, `closed.keys()`. If by "extract" you mean "remove", `del` or `.pop`. I really have no idea what you mean by "I need to extract the key" -- there are many keys, which one you need? All? Remove some specific one? Give code examples! Lots of us on SO are not English speakers, nobody manages to ask questions as incomprehensible as yours -- answering your Qs is not an exercise in Python, it's an exercise in blind guessing;-).
Alex Martelli