开发者

dictionary in python

开发者 https://www.devze.com 2023-01-07 08:09 出处:网络
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)]
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 par开发者_StackOverflow中文版ent 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?


(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".


Dictionaries do not preserve key order.

You would need to use an ordered dict substitute.

0

精彩评论

暂无评论...
验证码 换一张
取 消