2010-07-17 68 views
0
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)} 

为什么订单已更改? 5,4应该在第一个位置和(4,5)在小孩字典的第二个位置。python中的字典

而我该如何为密钥提供更多的价值?

这里我的关键是(5,4),它的值是南和1.现在我想要它的父节点也作为它的值,所以当我使用密钥[1] - 它不能给我南, 1和父节点

因为“s”只包含2个东西,南和1,所以它只产生2个值。我还需要1个。命令是什么?

+0

[Python项目中排序的项目]的可能重复(http://stackoverflow.com/questions/3127945/items-ordering-in-python-dictionary) – fortran 2011-12-09 12:49:22

回答

3

词典不保留键顺序。

您需要使用有序的字典替代。

10

(1)在Python字典中有无序。如果您需要维护广告订单,请使用OrderedDict(自Python 2.7和3.1起可用)。

(2)我不知道你的意思是“父节点”。