s = problem.getSuccessors(currNode)
print s
child = dict((t[0], t[1:]) for t in s)
print child
output of s = [((5, 4), 'South', 1), ((4, 5), 'West', 1)]
output of child = {(4, 5): ('West', 1), (5, 4): ('South', 1)}
Why the order has been changed?? 5,4 should be at first position and ( 4, 5) at 2nd position of child dict.
And how do I can put 1 more value for the key?
here my key is (5,4) and its values is south and 1. Now I want to have its parent node also as its value so that when I use the key[1]- it shud give me south, 1 and parent node
since "s" contains only 2 things,south and 1. so it is making only 2 values of the key. i need 1 more . what are the commands?
-
possible duplicate of Items ordering in Python dictionaryfortran– fortran2011年12月09日 12:49:22 +00:00Commented Dec 9, 2011 at 12:49
2 Answers 2
(1) In Python dictionaries are unordered. Use an OrderedDict (available since Python 2.7 and 3.1) if you need to maintain the insertion order.
(2) I don't know what you mean by "parent node".
Comments
Dictionaries do not preserve key order.
You would need to use an ordered dict substitute.