1

output of n is in the form:- ((6,5),'north',1)

I am making a dict child for it...in which (6,5) is the key and north and 1 are the values. I need to keep the (6,5) as the key and north as a direction....and I want to keep adding all the values till the while loop continues

asked Jul 18, 2010 at 2:38
1
  • 4
    It would help to see your whole while loop Commented Jul 18, 2010 at 2:43

3 Answers 3

2

If you want to keep all the key / value pairs in one dict (and all keys are distinct, of course):

totaldict = {}
for ...whatever your loop is...:
 ...
 totaldict.update(( t[0], t[1:]) for t in n )

If you want a list of dicts, @San's answer is good. If you want a single dict with not necessarily all distinct keys, and each key's corresponding value a list of tuples:

import collections
totaldict = collections.defaultdict(list)
for ...whatever your loop is...:
 ...
 for t in n:
 totaldict[t[0]].append(t[1:])

There may be other senses yet which you might mean "keep all the values of this dictioanary" to signify, but it is, as usual, impossible to guess precisely in what of the many possible meanings you intend it.

Edit: from the OP's edit (much-clarifying his question, though many murky aspects remain which I already asked about on some of his previous questions), he doesn't necessarily need one dict -- he needs to be able to trace any path backwards when he finally gets to a node that's deemed by the problem object to be "a solution" (or "goal").

The OP's edit to the Q now seems to mysteriously have disappeared, but if (as I dimly recall) he's skipping any node that's previously been pushed onto the stack, then one dict will do, because each node will be visited at most once (hence, no duplicate keys) -- however, that dict's entry, with the node as a key, should not point to the node's successors (signally useless in tracing the path backwards from the goal!), but to the predecessor which led to visiting this node (and the direction that was taken from that immediate predecessor to come to this node). The root's node entry should be empty (since it has no predecessor).

answered Jul 18, 2010 at 3:07
Sign up to request clarification or add additional context in comments.

1 Comment

@Shilpa: Very good, thanks. Another tip: If you find more meaningful titles to your questions (other than "Python X problem" or "X in Python") it also will help attracting people who might wish to answer them. I suggest you read catb.org/esr/faqs/smart-questions.html
1

It sounds like you want a list of dictionaries, but it's difficult to tell with so little context.

my_list = []
while some_loop_condition:
 child = dict(( t[0], t[1:]) for t in n )
 my_list.append(child)
answered Jul 18, 2010 at 2:47

Comments

0

It looks like you want to define 'child' outside if the loop, and reference it within:

e.g.:

child = {}
while blah:
 ...
 child.update(dict(( t[0], t[1:]) for t in n )
 ...
answered Jul 18, 2010 at 3:31

1 Comment

it will keep all the entries....but I need to provide only the list of directions which is at 2nd position of dictionary ((4,5):north, 1), (7,5):south,1). how to get those directions

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.