由于
OrderedDict按插入顺序排序,因此您必须创建一个新的。
在您的情况下,代码如下所示:
foo = OrderedDict(sorted(foo.iteritems(), key=lambda x: x[1]['depth']))
有关更多示例,请参见http://docs.python.org/dev/library/collections.html#ordereddict-
examples-and-
recipes。
请注意,对于Python 3,您需要使用
.items()而不是
.iteritems()。



